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

150
Visualizações
Change text in a div if another div has a specific attribute

I would like to change a text in a div if another div has a specific attribute, in my case data-value.

In the specific if a div has an attribute data-value="cars" another div should have the text "Buy".

In the specific if data-value="cars" I would like to change the text "1" into "Buy"

<div class="one">1</div>

Here the sample code

<div class="dynamic">
  <div class="content">
   <div data-value="cars">
   Cars
   </div>
   <div data-value="truck">
   Truck
   </div>
   <div data-value="moto">
   Moto
   </div>
 </div>
</div>



<div class="change">
 <div>
 1
 </div>
 <div>
 2
 </div>
</div>

How can I achieve that in JS?

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

0

you need to first perform a query on the existence of any element within the content having the data-value attribute of "cars"

use the document.querySelector() function

  if (document.querySelector('.dynamic .content [data-value="cars"]')) ...

then you need to set the text content of all the div elements within the change that have the value 1, using the .textContent attribute

document.querySelectorAll('.change div')
  .forEach(div => if( div.textContent=="1") div.textContent="Buy");

putting all pieces together

  if (document.querySelector('.dynamic .content [data-value="cars"]')) {
  document.querySelectorAll('.change div')
    .forEach(div => if(div.textContent=="1") div.textContent="Buy" );
  }
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