Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

onChange in createElement not working. React

Using react and tried to create an element with createElement.

const inputForEnglish = document.createElement("input");
inputForEnglish.type = "text";
inputForEnglish.placeholder = "English";
inputForEnglish.dataset.answernumber = answers.children.length + 1;
inputForEnglish.dataset.textfield = "english";
inputForEnglish.onChange = (e) => detectAnswerChange(e);

The last line doesn’t work. I have a function called "detectAnswerChange()" and it takes event as parameter. The onChange works completely fine when it’s done directly to an element.

I also tried inputForEnglish.onchange = "detectAnswerChange()", it doesn’t work.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

It creates the element and all the above things works completely fine, but the inputForEnglish.onChange = (e) => detectAnswerChange(e); doesn’t work.

>Solution :

inputForEnglish.oninput=(e)=>{
    //runs when the input changes
    console.log(e.target.value)
}
inputForEnglish.onchange=(e)=>{
    //runs only when the input is out of focus
    console.log(e.target.value)
}

use oninput for inputs

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading