How to delete a DIV if there is a word in another DIV in jQuery?

1

I'm trying to do the following condition:

$(document).ready(function() {
if($("#content .productName").html().indexOf("JBL","Oversound")==-1) {
 $("#pague-so").hide();}
});

In this case, I want the div # pay-so to be hidden if the div .productName contains the word JBL or Oversound in the text. As you can see, I did not succeed in the above condition. Could anyone give any suggestions?

    
asked by anonymous 23.02.2016 / 13:55

3 answers

0

An alternative to getting more configurable is to use regular expressions.

In the example REGEX.test () will return true if it contains the word Oversound or JBL and then you will do whatever treatment you need.

var texto = $('#product .productName').html();
var regex = /Oversound|JBL/;

if (regex.test(texto)) {
  $('#product #conteudo').hide();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divid="product">
  <div class='productName'>Woofer 12" Oversound OVS-X 5K2</div>
  <div id="conteudo">Conteúdo ou wathever</div>
</div>
    
23.02.2016 / 14:22
1

You will not be able to perform multiple queries using indexOf, however you can use a regular expression.

var testes = {};
testes.teste1 = "Nada a encontrar";
testes.teste2 = "JBL Motors";
testes.teste3 = "Oversound Waves";
testes.teste4 = "JBL Oversound";

var regex = /(JBL|Oversound)/gi;
for (var teste in testes) {
  console.log(testes[teste], testes[teste].match(regex));
}
$(document).ready(function() {
var regex = /(JBL|Oversound)/gi;
if(!$("#content .productName").html().match(regex)) {
 $("#pague-so").hide();}
});
    
23.02.2016 / 14:23
0

You can use :contains() , which checks whether the element contains the value passed as a parameter. See below how to use.

$(document).ready(function () {
  $('#button').click(function () {
  	if ($('.example:contains("Teste")').length > 0){
  	  	$('.hide').hide();
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divclass="example">
    Teste
</div>
<div class="hide">
    Esconder
</div>
<button id="button">
Clique
</button>
    
23.02.2016 / 14:32