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

110
Views
PureJS iframe load button

I load iframe with button, when i add a second button it doesn't work.

HTML:

<div class="iframe_container"></div>
<a href="javascript:;" class="button" data-id="/test">Test</a>

Script:

var button = document.querySelector(".button");
var iframeContainer = document.querySelector(".iframe_container");
    
button.addEventListener("click", function () {
  var dataId = button.getAttribute("data-id");
  if (dataId != null && dataId != undefined) {
    iframeContainer.innerHTML = "";
    iframeContainer.innerHTML =
      '<iframe src="' + dataId + '" title="" style="width:100%;"></iframe>';
   }
});

What i want to do=

<div class="iframe_container"></div>
<a href="javascript:;" class="button" data-id="/test">Test</a>
    
    
<div class="iframe_container"></div>
<a href="javascript:;" class="button" data-id="/foo">Test 2</a>

... but the second button is not working.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

document.querySelector only catch the first DOM matching element ! You need querySelectorAll + do a loop on all element with the class, so you can add a listener on each.

 var buttons = document.querySelectorAll(".button");
 var iframeContainers = document.querySelectorAll(".iframe_container");

    
    for (let i = 0; i < buttons.length; i++){
        buttons[i].addEventListener("click", function () {
            var dataId = buttons[i].getAttribute("data-id");
            if (dataId != null && dataId != undefined) {
                iframeContainers[i].innerHTML = "";
                 iframeContainers[i].innerHTML =
                    '<iframe src="' + dataId + '" title="" style="width:100%;"></iframe>';
            }
        });
    }
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!