All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

OnChange Event In JavaScript For Dropdown

Last Updated : Mar 11, 2024

OnChange Event In JavaScript For Dropdown

In this article we will show you the solution of OnChange event in JavaScript for dropdown, it takes only one step to use onchange event in javascript for dropdown. onchange event includes text input or dropdown list and it also allowing you to compile a function.

OnChange event also used for changing the elements of Html.It easily detecting a user’s response. onchange event is generally useful for validation, detecting user’s input etc.

Step By Step Guide On OnChange Event In JavaScript For Dropdown :-

<title> javascript</title>
    <h1> Select a language</h1>
    <select id=”language” onchange=”changelanguage()”>
     <option value= “hindi”>Hindi</option>
      <option value=”english”>English</option>
       <option value=”sanskrit”>Sanskrit</option>
         <div id=”res”></div>
                 function changelanguage(){
                                var language= document.getElementById(“language”).value;
                                var res= document.getElementById(“res”);
                                res.textContent= “ selected language:” + language;
  1. Firstly, we use <html> tag it is the beginning of Html document.
  2. <head> tag defines the head section of html document.
  3. <title> tag defines the title name of html document. In this code the title name is “javascript”.
  4. <body> tag defines the content of html document.
  5. <h1> tag in code used for define the heading text in html
  6. Next we use select element with id as a “language” . It is a dropdown list that the users have an options to language selecting.
  7. We have use three option element in this code. Option has a value include languages- Hindi, English, Sanskrit. Then we close the select tag
  8. Now, the <div> method in this code will display the selecting language.
  9. Then, we create a javascript file “script.js” here we can write all the javascript code
  10. In this code, we can use changelanguage() function to give the value of this language.
  11. After that we can define a variable inside the function named language.In this variable we can use getElementById() method to accessing the select element by its id attribute and update the value of property of selected option.
  12. Then, we can define another variable inside the function named res. It uses getElementById() to assigning a reference to res variable.
  13. Next, will displaying the text “selected language:” concatenate with the selected language that we selected.

Conclusion :-

In conclusion, Using onchange event in javascript for dropdown is an easy one method.

We can use select element to dropdown the menu and we can also use different- different option where user can select any one option in these. For example- options include hindi, English, Sanskrit.

User can select any one option in these included option.We can use changelanguage() function when the user selects a language, changelanguage() function is called.

It update the selected value and updating the content of <div> element .So, this method defines how we using onchange event in javascript for dropdown.

I hope this article on OnChange event in JavaScript for dropdown helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪