HTML | onchange Event Attribute

The onchange event attribute works when the value of the element changes and select the new value from the List. It is a part of event attribute. It is similar to oninput event attribute. But the difference is while oninput attribute event occurs immediately after the value of element changes, while onchange event occur when the element looses focus. This attribute is associated with <select> element.

Syntax:

<element onchange = "script">

Attribute Value: This attribute contains single value script which works when onchange attribute called.



Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <title>onchange event attribute</title>
        <style>
            body {
                text-align:center;
            }
            h1 {
                color:green;
            }
        </style>
    </head>
    <body>
        <h1>GeeksforGeeks</h1>
        <h2>onchange Event Attribute</h2>
        <p>Choose Subject:</p>
        <select id="GFG" onchange="Geeks()">
        <option value="Data Structure">Data Structure
        <option value="Algorithm">Algorithm
        <option value="Computer Network">Computer Network
        <option value="Operating System">Operating System
        <option value="HTML">HTML
        </select>
        <p id="sudo"></p>
        <script>
            function Geeks() {
                var x = document.getElementById("GFG").value;
                document.getElementById("sudo").innerHTML = 
                "Selected Subject: " + x;
            }
        </script>
    </body>
</html>                    

chevron_right


Output:

Supported Browsers: The browser supported by onchange event attribute are listed below:

  • Apple Safari
  • Google Chrome
  • Firefox
  • Opera
  • Internet Explorer


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.