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

114
Visualizações
replacing tag with another tag targeting class

Trying to change the h3 tag to a <button> tag targeting via .container and .button-1 class using JavaScript. Unfortunately, it only targets the first h3

var allWindow = document.querySelector('.container .button-1 h3');

allWindow.outerHTML = '<button>submit</button>';
<div class="container">
<div class="button-1">
<h3>submit</h3>
</div>
</div>

<div class="container">
<div class="button-1">
<h3>submit</h3>
</div>
</div>

<div class="container">
<div class="button-1">
<h3>submit</h3>
</div>
</div>

<div class="container">
<div class="button-1">
<h3>submit</h3>
</div>
</div>

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

0

Your problem is you're using querySelector which is always referred to the first-found element. I'd suggest that you should change it to querySelectorAll (get all elements) and use a loop to update all your elements

var allWindows = document.querySelectorAll('.container .button-1 h3');

for(let element of allWindows) {
   element.outerHTML = '<button>submit</button>';
}
<div class="container">
  <div class="button-1">
    <h3>submit</h3>
  </div>
</div>

<div class="container">
  <div class="button-1">
    <h3>submit</h3>
  </div>
</div>

<div class="container">
  <div class="button-1">
    <h3>submit</h3>
  </div>
</div>

<div class="container">
  <div class="button-1">
    <h3>submit</h3>
  </div>
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

const replaceAll = () => {
    const elems = document.querySelectorAll('.container > .button-1 > h3');

    [...elems].forEach((elem) => {
        const button = document.createElement('button');
        button.textContent = 'submit';

        elem.replaceWith(button);
    });
};

document.querySelector('#replace').addEventListener('click', replaceAll);
<div class="container">
    <div class="button-1">
        <h3>submit</h3>
    </div>
</div>

<div class="container">
    <div class="button-1">
        <h3>submit</h3>
    </div>
</div>

<div class="container">
    <div class="button-1">
        <h3>submit</h3>
    </div>
</div>

<div class="container">
    <div class="button-1">
        <h3>submit</h3>
    </div>
</div>
<button id="replace">Replace</button>

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