Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Help with JQuery selector :not(:last)

Tags:

I'm trying to select all the bullets except the last bullet but I am doing something incorrectly.

Here is my html

<ul> <li>Text</li> <li>Text</li> <li>Text</li> <li>Text</li> <li>Text</li> </ul>  <ul> <li>Text</li> <li>Text</li> <li>Text</li> <li>Text</li> <li>Text</li> </ul>   <ul> <li>Text</li> <li>Text</li> <li>Text</li> <li>Text</li> <li>Text</li> </ul> 

And my jQuery:

jQuery('ul li:not(:last)').append(" | "); 

It's adding the pipe to every bullet however...

like image 207
redconservatory Avatar asked Apr 12 '11 20:04

redconservatory


People also ask

How can select last column of table using jQuery?

Using the $('td:last') selector just selects the last td tag, i.e., c4.

Which CSS selectors can you not use in jQuery?

Given a list of elements and the task is to not select a particular class using JQuery. jQuery :not() Selector: This selector selects all elements except the specified element. Parameters: It contains single parameter selector which is required.

Which jQuery selector is fastest?

ID and Element selector are the fastest selectors in jQuery.


1 Answers

Because your html contains multiple unordered lists, $('ul li') will selects every single <li>, in all of the <ul>s. :last then selects the final element in that list. Use :last-child, which gets the last <li> in each of the <ul>s.

$('ul li:not(:last-child)').append(' | '); 

http://jsfiddle.net/mattball/qmVdb/1/

like image 173
Matt Ball Avatar answered Sep 20 '22 12:09

Matt Ball