user31782 user31782 - 2 years ago 83
HTML Question

How to select last element with jquery?

I want to select the last

element on my webpage I tried:

$(" audio:last-of-type");

The problem is that it returns all the

Object { length: 12, prevObject: Object, context: HTMLDocument → index.html, selector: " audio:last-of-type", 12 more… }

There are 12 audio elements on the page and the jquery selector returns all of them.

So, How do I select last element with jquery?

Answer Source

Use last(). You could use the :last selector too, but then it can't be passed directly to the browser's selector engine.


Why doesn't last-of-type work?

The jQuery docs for the selector says:

Selects all elements that are the last among siblings of the same element name.

So in your case it would be matching all the last audio elements that were inside of other elements.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download