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

204
Visualizações
Assign the same text in all the same ID or Class

On my pages it repeats short content like image licenses, or other things. The convenience would also be to be able to edit them all at the same time by typing just one. I know it's against SEO, but I don't really care about search engine penalties. I can't get this javascript to work. And yet I think I wrote it right.

<script type='text/javascript'>
//<![CDATA[
  var x = document.getElementById("license").document.querySelectorAll("#license");
  var i;
  var text = "Hello world";
    for (i = 0; i < x.length; i++){
     x[i].innerHTML = text; 
  }
//]]> 
</script>

about 4 years ago · Santiago Gelvez
3 Respostas
Responde à pergunta

0

Try this, but dont use more than one ID. Some codes can work but generally its a bad idea, better to use classes for things like this.

var mytxt='lalal';

document.querySelectorAll(".test,#blah").forEach(function(elem){
  elem.value=mytxt;
});
<input type='text' class='test' readonly>
<input type='text' class='test' readonly>
<input type='text' class='test' readonly>

<input id='blah' type='text' readonly>
<input id='blah' type='text' readonly>
<input id='blah' type='text' readonly>

about 4 years ago · Santiago Gelvez Relatório

0

If I understand your requirement correctly, then the following should achieve it.

let something = document.getElementsByClassName('something');
let button = document.getElementById('button');

// Convert to actual array
something = Array.from(something);

const update = (val) => {
  something.forEach(item => {
    item.innerHTML = val
  })
}

button.addEventListener('click', () => update('Updated everything'))
<div class="something">SOMETHINGGGGG</div>
<div class="something">SOMETHINGGGGG</div>
<div class="something">SOMETHINGGGGG</div>
<div class="something">SOMETHINGGGGG</div>
<div class="something">SOMETHINGGGGG</div>
<div class="something">SOMETHINGGGGG</div>
<div class="something">SOMETHINGGGGG</div>
<div class="something">SOMETHINGGGGG</div>
<div class="something">SOMETHINGGGGG</div>
<div class="something">SOMETHINGGGGG</div>
<div class="something">SOMETHINGGGGG</div>
<div class="something">SOMETHINGGGGG</div>
<div class="something">SOMETHINGGGGG</div>
<div class="something">SOMETHINGGGGG</div>

<button id='button'>UPDATE</button>

about 4 years ago · Santiago Gelvez Relatório

0

First of all querySelectorAll selects one or more elements and returns it as a list of elements.

You are selecting elements by id with this ("#license") so it returns only the first element with id="license".

To get a list of elements you should select them by class or name like this,

By class document.querySelectorAll(".license")

By name document.querySelectorAll("[name="license"]")

I recommend you to read more about Document.querySelectorAll()

about 4 years ago · Santiago Gelvez 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