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

Asked

Viewed 149 times

1

I’m trying to make the following condition:

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

In this case, I want the div #pagar-so to be hidden if div . productName contains the word JBL or Oversound in the text. As you can see, I was unsuccessful in the above condition. Could someone make a suggestion?

3 answers

1

you will not be able to perform multiple queries using the index, but 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();}
});
  • Tobymosque and Diego F, I could not insert this condition on the page I need. This page would be: http://www.premiershop.com.br/caixa-de-som-portatil-jbl-go-wireless-1173700/p

0


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

In the example the REGEX.test() will return true if it contains the word Oversound or JBL and then you’ll get the 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>
<div id="product">
  <div class='productName'>Woofer 12" Oversound OVS-X 5K2</div>
  <div id="conteudo">Conteúdo ou wathever</div>
</div>

  • Pedro, I couldn’t apply that condition. This page is: http://www.premiershop.com.br/caixa-de-som-portatil-jbl-go-wireless-1173700/p

  • Try to make the most of it when you have a problem Jean, it helps the community to help you. On your productName is a class and not a id. Try this $('.productName')

  • Pedro I apologize for the inconvenience, however the code I left in the question, productName was already informed as a class. Anyway, thank you so much for your support! I tested the code and it worked with the following condition: $("Document"). ready(Function() { var text = $('#content . productName'). html(); var regex = /Oversound|JBL/; if (regex.test(text)) { $('#pay-so'). Hide(); }});

  • Great. :) Now remember to mark colleagues' responses as useful and accept one too if they answered your question.

  • 1

    I don’t have a reputation yet :/ but when I do, I will gladly do so. Thank you again Peter.

0

You can use the :contains(), that checks if the element contains the value passed as 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>

<div class="example">
    Teste
</div>
<div class="hide">
    Esconder
</div>
<button id="button">
Clique
</button>

Browser other questions tagged

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