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

236
Visualizações
how to create <input onchange='myFunction()' /> dynamically with function

I know that you can use document.createElement for this but I get a domexception from doing this:

document.createElement("<input onchange='myFunction()' />");

Error:

Uncaught DOMException: Failed to execute 'createElement' on 'Document': The tag name provided ('<input onchange='myFunction()' />') is not a valid name.

Do you know how to create an element dynamically with a function?

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

0

You should make it in 3 times :

  • First creating the element
  • Then adding the function
  • Finally adding it to the dom

const myFunction = () => {
  console.log("hello world")
}

const btn = document.createElement('button')
btn.innerText = "MyBtn"
btn.addEventListener('click', myFunction)
document.body.appendChild(btn)

about 4 years ago · Juan Pablo Isaza Relatório

0

Sorry I was too quick to ask for help. But this is the answer:

newInput3.onchange = myFunction;

myFunction should not be an actual string.

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