Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery get element containing child element with specific html value

Tags:

jquery

I would like to use Jquery to add a class to "li" element that contains a "span" element with a html/val equal to zero.

For Example if my code looks like this:

<ul>
    <li><span>Item 1</span><span class="num">30</span></li>
    <li><span>Item 2</span><span class="num">0</span></li>
    <li><span>Item 3</span><span class="num">20</span></li>
</ul>

I want to change it to the following:

<ul>
    <li><span>Item 1</span><span class="num">30</span></li>
    <li class="disabled"><span>Item 2</span><span class="num">0</span></li>
    <li><span>Item 3</span><span class="num">20</span></li>
</ul>

In the past I have used code to check elements attribute values, but never their html/val doing something to this effect...

$('li').has('span.num').addClass('disabled');

However in this case that would result in:

<ul>
    <li class="disabled"><span>Item 1</span><span class="num">30</span></li>
    <li class="disabled"><span>Item 2</span><span class="num">0</span></li>
    <li class="disabled"><span>Item 3</span><span class="num">20</span></li>
</ul>

Which is obviously not going work... Thanks

like image 737
Jeremy A. West Avatar asked Feb 01 '12 19:02

Jeremy A. West


People also ask

Can we get the children element from the parent element using jQuery?

jQuery children() method is used to get the direct children of the selected HTML element. You can use children() method to traverse through the child elements of the selected parent element.

How do you get the children of the $( this selector?

Answer: Use the jQuery find() Method You can use the find() method to get the children of the $(this) selector using jQuery. The jQuery code in the following example will simply select the child <img> element and apply some CSS style on it on click of the parent <div> element.

How do you get children of children in jQuery?

jQuery children() MethodThe children() method returns all direct children of the selected element. The DOM tree: This method only traverse a single level down the DOM tree. To traverse down multiple levels (to return grandchildren or other descendants), use the find() method.

How do you select the nth child in jQuery select all p elements which are the 3rd child of their parent?

jQuery :nth-child() SelectorThe :nth-child(n) selector selects all elements that are the nth child, regardless of type, of their parent. Tip: Use the :nth-of-type() selector to select all elements that are the nth child, of a particular type, of their parent.


1 Answers

This should do what you want.

$('li').each(function(){
    if( $(this).find('span.num').text() == '0' ) $(this).addClass('disabled')
});

JS Fiddle

I would have suggested the following:

$('li').has('span.num:contains(0)').addClass('disabled')

But it doesn't work, as it checks if the value exists inside the html — not for an exact match. In this case, each of the span.num elements have a 0 in them, so every li would get the selected class. There doesn't seem to be an :equals() counterpart to the :contains() selector.

like image 59
Sam Sehnert Avatar answered Sep 20 '22 18:09

Sam Sehnert