2

I want to hide an element and a label based on value: My html code looks like this

<form id="wrapper">
    <label>
        <input type="checkbox" value="Slider">
        Slider
    </label>
</form>

So using jquery i can find an input value that contains value="Slider" but the label still remains, because it it doesn't contain any id or class and I can't add anything there, so how can I hide it

$('#wrapper').find("input[value='Slider']").each(function(){
    $(this).hide()
});

Here is a JSFIDDLE.

Mohammad
  • 21,175
  • 15
  • 55
  • 84
Babel
  • 589
  • 2
  • 9
  • 23

3 Answers3

5

You need to hide the parent label element to hide input element and its sibling text:

$('#wrapper').find("input[value='Slider']").closest('label').hide();
Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
2

Use .has() method to checking children of element.

$("label").has("input[value='Slider']").hide();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>
    <input type="checkbox" value="Slider">
    Slider
</label>
<label>
    <input type="checkbox" value="Slider2">
    Slider2
</label>
Mohammad
  • 21,175
  • 15
  • 55
  • 84
  • can I do the same this using javascript without jquery cause I'm having scripts conflicts something like:document.getElementById(' ').style.display = 'none'; – Babel Jul 06 '16 at 07:53
1

You can try this:

    $('#wrapper').find("input[value='Slider']").each(function(){
          // $(this).hide();  If you want to hide input too
           $(this).parent().hide();

});
Poonam
  • 549
  • 4
  • 15