I want to select all li elements from ul having class "one" only
<ul class="one">
<li class="heading">
<li class="list">
<li class="list">
<li class="list">
<li class="list">
</ul>
<ul class="one two ">
<li class="heading">
<li class="list">
<li class="list">
<li class="list">
<li class="list">
</ul>
In jQuery, the class and ID selectors are the same as in CSS. If you want to select elements with a certain class, use a dot ( . ) and the class name. If you want to select elements with a certain ID, use the hash symbol ( # ) and the ID name.
In jQuery, we can select elements with multiple classes using the . class selector.
Easiest:
$('ul[class="one"] li');
Alternative, select .one
but not .two
:
$('ul.one:not(.two) li');
You can use the jquery filter function to get a count of how many classes have been assigned to the element. When looking for all UL's with class 'one' only then the number of unique classes against the item will be 1. Once you have the filtered UL's you can simply select the sub LI's.
var listItems =
$('ul.one')
.filter(function(index) {
return $(this).attr('class').split(' ').length == 1;
})
.find('li');
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