How can i read the last element id using javascript?
for example :
<div id='print'>
<p id=1>some data</p>
<p id=2>some data</p>
<p id=3>some data</p>
<p id=4>some data</p>
<p id=5>some data</p>
</div>
I need to detect the id of last
I need max number of the id actually.it’s generated sequentially.
First off, id values cannot be numbers. They must start with a non-number.
You can get the last child with this plain javascript (no jQuery required):
or in one line, it would be this:
FYI, in case you need access to any other children you can always get all the children and then fetch the one you want:
Now that you’ve explained what you’re really trying to do, I’d suggest this:
And, then this javascript:
Using the
data-xxxformat is forward compatible with the HTML5 specification and usable in older browsers withgetAttribute().