I am trying to grab that 15
at the end of the href with jquery..
<a class="ajax" href="/organizations/1/media/galleries/15">Silly Gangs</a>
Any ideas?
I am trying to grab that 15
at the end of the href with jquery..
<a class="ajax" href="/organizations/1/media/galleries/15">Silly Gangs</a>
Any ideas?
You don't need jQuery
for this, just vanilla JavaScript:
var href = someLinkElement.href;
var lastIndex = href.lastIndexOf('/') + 1;
var lastComponent = href.substring(lastIndex);
var num = $("a.ajax").attr("href").replace(/.*\/(\d+)$/, "$1");
Explanation of the regex:
.* # anything (this runs right to the end of the string)
\/ # a slash (backtracks to the last slash in the string)
(\d+) # multiple digits (will be saved to group $1)
$ # end-of-string anchor (ensures correct match)
The replace()
call will substitute the entire input string with the number at its end, whatever it might be. It will return ""
if there's no number at the end.
Oh, and as added bonus here is something that uses jQuery some more, modify as needed:
$("a.ajax").each(function () {
$(this).data("num", /* store in data for later re-use */
$(this).attr("href").replace(/.*\/(\d+)$/, "$1")
);
});
$(".ajax").each( function() {
var lastElementOfHREF = $(this).attr("href").split("/").pop();
// now you do something here with this lastElement...
});
You can do it like this:
var Value=$("a.ajax").attr("href").split("/").pop();
It's faster than using regex.