JavaScript onchange Event

You are Here:

JavaScript onchange Event

The onchange event occurs when the value of an element has been changed.

onchange Event as Attribute

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <p>Try to change the value in select tag</p> <select id="mySelect" onchange="myFunction()"> <option>Harley Davidson</option> <option>Honda</option> <option>Suzuki</option> <option>Yamaha</option> </select> <p id="point"></p> <script> function myFunction() { var x = document.getElementById("mySelect").value; document.getElementById("point").innerHTML = "You selected: "+x; } </script> </body> </html>

Syntax

As Attribute

<element onchange = "JavaScript">

As Property

object.onchange = function(){ // code };

Using Event Listener

object.addEventListener("change" , myScript);

onchange Event as Property

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <p>Try to change the value in select tag</p> <select id="mySelect"> <option>Harley Davidson</option> <option>Honda</option> <option>Suzuki</option> <option>Yamaha</option> </select> <p id="point"></p> <script> var x = document.getElementsByTagName("select")[0]; function myFunction() { var x = document.getElementById("mySelect").value; document.getElementById("point").innerHTML = "You selected: "+x; } x.onchange = myFunction; </script> </body> </html>

Using addEventListener() Method

Note: The addEventListener() method is not supported in Internet Explorer 8 and below.

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <p>Try to change the value in select tag</p> <select id="mySelect"> <option>Harley Davidson</option> <option>Honda</option> <option>Suzuki</option> <option>Yamaha</option> </select> <p id="point"></p> <script> var x = document.getElementsByTagName("select")[0]; function myFunction() { var x = document.getElementById("mySelect").value; document.getElementById("point").innerHTML = "You selected: "+x; } x.addEventListener("change", myFunction); </script> </body> </html>

Reminder

Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in JavaScript.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author