Mythriel Mythriel - 1 year ago 77
HTML Question

Select an element based on its html value in jQuery

I have the following markup:

<ul class="list">

How can I select the li items with the html value of 5 and 6 and add a class to them?

Answer Source

One option is the :contains selector...


However, since it's just looking for that text and not matching the entire contents, that will also match <li>56</li>, <li>16</li>, and so on.

So, what you probably ought to do instead is use .filter() like so:

$("li").filter(function () {
    var text = $(this).text();
    return text === "5" || text === "6"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download