Note that there are some explanatory texts on larger screens.

plurals
  1. POJquery get element containing child element with specific html value
    text
    copied!<p>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.</p> <p>For Example if my code looks like this:</p> <pre><code>&lt;ul&gt; &lt;li&gt;&lt;span&gt;Item 1&lt;/span&gt;&lt;span class="num"&gt;30&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span&gt;Item 2&lt;/span&gt;&lt;span class="num"&gt;0&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span&gt;Item 3&lt;/span&gt;&lt;span class="num"&gt;20&lt;/span&gt;&lt;/li&gt; &lt;/ul&gt; </code></pre> <p>I want to change it to the following:</p> <pre><code>&lt;ul&gt; &lt;li&gt;&lt;span&gt;Item 1&lt;/span&gt;&lt;span class="num"&gt;30&lt;/span&gt;&lt;/li&gt; &lt;li class="disabled"&gt;&lt;span&gt;Item 2&lt;/span&gt;&lt;span class="num"&gt;0&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span&gt;Item 3&lt;/span&gt;&lt;span class="num"&gt;20&lt;/span&gt;&lt;/li&gt; &lt;/ul&gt; </code></pre> <p>In the past I have used code to check elements attribute values, but never their html/val doing something to this effect...</p> <pre><code>$('li').has('span.num').addClass('disabled'); </code></pre> <p>However in this case that would result in:</p> <pre><code>&lt;ul&gt; &lt;li class="disabled"&gt;&lt;span&gt;Item 1&lt;/span&gt;&lt;span class="num"&gt;30&lt;/span&gt;&lt;/li&gt; &lt;li class="disabled"&gt;&lt;span&gt;Item 2&lt;/span&gt;&lt;span class="num"&gt;0&lt;/span&gt;&lt;/li&gt; &lt;li class="disabled"&gt;&lt;span&gt;Item 3&lt;/span&gt;&lt;span class="num"&gt;20&lt;/span&gt;&lt;/li&gt; &lt;/ul&gt; </code></pre> <p>Which is obviously not going work... Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload