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

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

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 Report

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