I have 2 divs, with the same id, pagination, one is at the top of the page, the other at the bottom.
What I'd like to do, is find the last id, so this would be the <div id="pagination"></div>
at the bottom of the page and add some more HTML, so it looks like:
<div id="pagination"></div><hr />
Is this possible in jQuery?
The last() function is an inbuilt function in jQuery which is used to find the last element of the specified elements. Here selector is the selected elements. Parameters: It does not accept any parameter. Return value: It returns the last element out of the selected elements.
The :last selector selects the last element. Note: This selector can only select one single element. Use the :last-child selector to select more than one element (one for each parent). This is mostly used together with another selector to select the last element in a group (like in the example above).
jQuery last() Method The last() method returns the last element of the selected elements. Tip: To return the first element, use the first() method.
You cant use same ID in one page. The ID is unique identifier, If you need use identifier more than once, use class instead.
Any way, you can use :last selector, like this:
$("div:last")
You could eaven use :last-child, like this:
$('#pagination:last-child')
If you were being correct, you should never have two ID's the same, so using $('#selector:last')
or $('#selector').last()
won't work, however you can cheat a little like this:
$("div[id=pagination]:last").......
Here is an example
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With