Skip to content
Advertisement

onchange property doesn`t work in javascript

I want to launch the function test() if the user inputs something in the html input field with the id="sc1dc1" (without using the "onchange=" directly in HTML). What is wrong?

HTML:

JavaScript

Javascript:

JavaScript

Advertisement

Answer

The thing about onchange events is that they only fire when the <input> field loses focus. If you want an immediate response, you should use oninput like so:

JavaScript

You also need to make x equal to the actual element, not the value of the element. Removing:

JavaScript

From this line:

JavaScript

Will fix it.

Demonstration with onchange:

JavaScript
JavaScript

Demonstration with oninput:

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement