Hide Div Jquery

Asked

Viewed 460 times

1

How can I do to if there is value occur a None display on all Divs with car?

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>

<script>
$(document).ready(function() {
    $(".acionador").click(function() {
        if($(".texto").html().indexOf("carro")!=-1) {
            // fazer um hide em todas as divs que contem carro
        }
    });
}); 
</script>

<div class="acionador" style="background: black; color: white;">acionador</div>

<div class="texto"> texo carro </div>
<div class="texto"> texo carro </div>
<div class="texto"> texo moto </div>

2 answers

4


You can use the selector contains:

$("div.texto:contains('carro')").hide();

$(document).ready(function() {
  $(".acionador").click(function() {
    $("div.texto:contains('carro')").hide();
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="acionador" style="background: black; color: white;">acionador</div>

<div class="texto"> texto carro </div>
<div class="texto"> texto carro </div>
<div class="texto"> texto moto </div>

  • 1

    Great! I didn’t know this contains :)

  • @Josimara despite not having the contains listed, here is a list of 30 useful selectors.

  • as specific to div by class or id ai?

  • @Josimara by class you use the ., as in the example div.texto. For id you use the #, getting something like div#texto1

  • Sorack, it’s been so long...

  • 1

    @Miguel hahaha was dating, so there was no time. Now I took a kick in the ass and more available

Show 1 more comment

1

You can use the method each jQuery to iterate with all occurrences of the text class for example:

$(document).ready(function() {
    $(".acionador").click(function() {
        $(".texto").each(function() {
            if($(this).html().indexOf("carro")!=-1) {
                $(this).css('display', 'none');
            }
        });
    });
}); 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.