Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

170
Visualizações
how to check if an html element has certain text with javascript

I have an html p tag as showen below

   <p class="card-about-bambo-number card-aboot-mahagany-number">0 <spam>left</spam></p> 

I also have a button that I would like to be disabled when the p tag has a 0 in.

I have tried doing this both within JavaScript and jquery.

I tried using includes but this didnt work, I have also tried using contains.

Here is the code I have tired

$(document).ready(() => {


  if ($(".card-about-mahagany-number:contains('0')")) {
    $('.btn-about-mahagany').attr('disabled', 'disabled');
    $('.card-about-mahagany-body').addClass('card-zero');
  }

});

const numberText = document.getElementsByClassName('card-aboot-mahagany-number');
const buttonOut = document.getElementsByClassName('btn-about-mahagany');
//buttonOut.disabled = true;

if (numberText.textContent.includes('0')) {
  buttonOut.disabled = true;
}
<p class="card-about-bambo-number card-aboot-mahagany-number">0 <spam>left</spam></p> 
   
    <button class="btn btn-select btn-about-mahagany btn-primary" disabled="false">Out of Stock</button>
   

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

document.getElementsByClassName returns an array-like object of the elements with that class.

Try this:

for (let i = 0; i < numberText.length; i++) {
  if (numberText[i].textContent.includes("0")) {
    for (let i = 0; i < buttonOut.length; i++) {
      buttonOut[i].disabled = true;
    }
  }
}
about 4 years ago · Juan Pablo Isaza Relatório

0

First, lets change from <spam> to <span>.

Second, we try to create a simple code for one card only. In this case, if you change the value from 0 to 1, you will see the button. If you change the value from 1 to 0, the button will not show up when you run it.

$(document).ready(() => {
  console.log('document ready');
  
  let card = $('p.card-about-bambo-number');
    
  console.log(card.text());
  
  let value = +card.text().replace(/\D+/g, "");
  console.log('value: ', value);
  console.log('value type: ', typeof(value));
  
  $('#btn').toggle(value > 0);
  
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p class="card-about-bambo-number card-aboot-mahagany-number">0 <span>left</span></p>
<button id="btn">Button</button>

Third, lets modify this to work with more than one card:

$(document).ready(() => {
      
      let cards = $('p.card-about-bambo-number');
      
      cards.each(function() {
        let card = $(this);
        
        let value = +card.text().replace(/\D+/g, "");
        
        card.next('#btn').toggle(value > 0);
      });
        
      
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<p class="card-about-bambo-number card-aboot-mahagany-number">0 <span>left</span></p>
<button id="btn">Button</button>

<hr/>

<p class="card-about-bambo-number card-aboot-mahagany-number">1 <span>left</span></p>
<button id="btn">Button</button>

Using the class card-about-bambo-number, we will iterate throughout all the elements that have that class.

For each card, we will use the method text to extract the text, then we use regular expression to remove anything that isn't a digit, next we convert the number from a string to a number using + in front (a "javascript hack"), and with that we obtain the number.

Finally, we indicate that with want the next sibling to that element containing such class, to toggle (show/hide) depending if the value is higher than zero.

I hope this helps out.

about 4 years ago · Juan Pablo Isaza Relatório

0

textcontent should be innerHTML

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda