I have a jQuery id like
<div id="myid-page-top-0">
some stuff
</div>
I want to use jQuery to grab the "0"
from the end of the #id
. I have tried like
$('div').slice(-1,0);
But this doesn't work ? Any ideas ?
I have a jQuery id like
<div id="myid-page-top-0">
some stuff
</div>
I want to use jQuery to grab the "0"
from the end of the #id
. I have tried like
$('div').slice(-1,0);
But this doesn't work ? Any ideas ?
Instead of slice, which would only work if your id contains numbers 0-9, not if it is say 10 or 11, you should base it off of the dashes.
Something like:
var id = "myid-page-top-0";
alert(id.split("-")[3]);
or in your code something like:
var number = $('div').attr('id').split("-")[3];
$('div')
is an object, you want the ID:
$('div').attr('id').slice(-1,0);
If your numbers go into double digits though you might run into problems, it might be better to do it like this:
$('div').attr('id').replace('myid-page-top-', '');
Here you go buddy. Tested and working. iterates through all divs and returns the ending number
<script type="text/javascript">
$(document).ready(function() {
$('div').each(function(i) {
var id = $(this).attr("id");
var slices = id.substr(id.length - 1, 1);
alert(slices);
});
}
);
</script>
<div id="myid-page-top-0">
some stuff
</div>
<div id="myid-page-top-1">
some stuff
</div>
<div id="myid-page-top-2">
some stuff
</div>