Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find html element by content and hide using jQuery

Tags:

jquery

find

hide

I am trying to use jquery to find some elements in some HTML, I would like to find and hide the list id that contains the label text 'This is my test label' and 'Yest another test label

<ul id="mylist" class="top_level_list">
<li id="field66" class="myfield">
    <div class="field_icons">
        <div class="title">This is my title</div>
    </div>
    <label class="my_label" for="input71">This is my test label</label>
</li>
<li id="field20" class="myfield">
    <div class="field_icons">
        <div class="title">This another test title</div>
    </div>
    <label class="my_label" for="input71">Yet another test label</label>
</li>

I have found the jQuery .hide function as well as the .find() function but i'm unsure as to how to select the element using the content within it.

Can anyone help?

like image 844
fightstarr20 Avatar asked Dec 21 '22 04:12

fightstarr20


2 Answers

This is a slightly easier solution. $(".my_label:contains('This is my test label')").hide();. You can see it in action here: http://jsfiddle.net/kPxTw/

like image 158
Tim Wasson Avatar answered Dec 22 '22 18:12

Tim Wasson


Have a look here: http://jsfiddle.net/yPFgu/1/

CODE

var labelsToHide = ["This is my test label", "Another one"];

$("li").each(function () {
    var txt = $(this).find("label").text();
    if (labelsToHide.indexOf(txt) > -1 )
        $(this).hide();
});

Hope it helps.

like image 40
BeNdErR Avatar answered Dec 22 '22 17:12

BeNdErR