Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - find last occurrence of a div

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?

like image 245
terrid25 Avatar asked Mar 31 '11 10:03

terrid25


People also ask

How to find last div 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.

How to select last element in jQuery?

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).

How to get last element of class jQuery?

jQuery last() Method The last() method returns the last element of the selected elements. Tip: To return the first element, use the first() method.


2 Answers

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')
like image 195
Deele Avatar answered Oct 12 '22 06:10

Deele


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

like image 31
Scoobler Avatar answered Oct 12 '22 05:10

Scoobler