Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

113
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!