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

297
Visualizações
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 Respostas
Responde à pergunta

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 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