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

296
Views
Changing Iframe size by click event listener not working

I'm trying to change the iframe size by clicking the button by using the click event listener. But the iframe auto runs the function and I haven't even clicked the button...

Someone said this is a duplicate of How to pass arguments to addEventListener listener function? but I don't see where is this even related???

const btn = document.querySelector('button');
const iframe = document.querySelector('iframe');

function change(w) {
   iframe.style.width = `${w}px`;
}

btn.addEventListener('click', change(150));
iframe {
  width: 300px;
  height: 300px;
  background-color: blue;
}
<button>Change</button>
<iframe src=""></iframe>

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

change your event listener function like so

btn.addEventListener('click', () => change(150));

OR

btn.addEventListener('click', function() { change(150) });

addEventListener expects a function definition and not a function call as second argument.

For more read: https://www.w3schools.com/js/js_htmldom_eventlistener.asp

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