×

JavaScript dropdown onchange

What is onchange event?

It is an event in JavaScript used to make the web pages dynamic. The onchange event gets triggered whenever the value of an event changes. It usually occurs whenever the element loses focus.

This event is supported by mostly all the browsers like Google chrome, safari, Microsoft Edge/Internet Explorer, Oracle, Firefox etc.

NOTE: In case of radio buttons and checkboxes, the onchange event gets triggered whenever the checked state is changed.

What is the difference between oninput and onchange?

  • The main difference is that the oninput event gets triggered immediately after the value of an element has changed, whereas the onchange event gets triggered whenever the element or the object loses focus, after the content has been modified.
  • The other major difference can be, that the onchange event also functions on the <select> elements, whereas oninput does not.

What is a drop-down list?

It is a graphical control element. It resembles similarity to a list box. A drop-down list lets the user choose one value from a list. Whenever a drop-down list is in inactive state, it shows only a single value. Whenever the drop-down lists are activated, they display a list of values, from which the user can select one value.

How are values selected using onchange from a Drop-down list?

A JavaScript onchange event handler is usually assigned to the HTML Select DropDownList.

The GetSelectedTextValue JavaScript function gets executed whenever an item is selected in the HTML Select DropDownList.

The reference of the HTML Select DropDownList is passed as parameter to the onchange event.

This reference helps to determine the selected Text and Value, which is then displayed using JavaScript alert message box.

The onchange event can be used commonly in three ways for making dropdowns:

  • In HTML,

Syntax:

<element onchange= functionname>


<script>
function functionname(){
	//definition of the function
}
</script>

Example:

<!DOCTYPE html>
<html>
<body>


<p>Select a programming language from the list.</p>


<select id="myList" onchange="myFunctiondemo()">
  <option value="C++">C++</option>
  <option value="Python">Python</option>
  <option value="Java">Java</option>
  <option value="JavaScript">JavaScript</option>
</select>


<p>When you select a programming language, a function is triggered which outputs the value of the selected language.</p>


<p id="funcdemo"></p>


<script>
function myFunctiondemo() {
  var x = document.getElementById("myList").value;
  document.getElementById("funcdemo").innerHTML = "The new selection is: " + x;
}
</script>


</body>
</html>

Output:

JavaScript Dropdown Onchange

The language keeps changing on selecting different languages, for example if we choose C++,

JavaScript Dropdown Onchange
  • In JavaScript,

Syntax:

<script>
object.onchange = function(){definition};


function functionname(){
	//definition of the function
}
</script>

Example:

<!DOCTYPE html>
<html>
<body>


<p>Select a programming language from the list.</p>


<select id="myList">
  <option value="C++">C++</option>
  <option value="Python">Python</option>
  <option value="Java">Java</option>
  <option value="JavaScript">JavaScript</option>
</select>


<p>When you select a programming language, a function is triggered which outputs the value of the selected language.</p>
<p id="funcdemo"></p>




<script>
document.getElementById("myList").onchange = function() {myFunctiondemo()};


function myFunctiondemo() {
  var x = document.getElementById("myList").value;
   document.getElementById("funcdemo").innerHTML = "The new selection is: " + x;
}
</script>


</body>
</html>

Output:

JavaScript Dropdown Onchange
  • Using addEventListener() method in JavaScript

Syntax:

<script>
object.addEventListener("change", myScript);
function functionname(){
	//definition of the function
}
</script>

Example:

<!DOCTYPE html>
<html>
<body>
<p>Select a fruit from the list.</p>


<select id="myList">
  <option value="Apple">Apple</option>
  <option value="Orange">Orange</option>
  <option value="Pear">Pear</option>
  <option value="Banana">Banana</option>
</select>


<p>When you select a fruit from the dropdown, a function is triggered which outputs the value of the selected fruit.</p>
<p id="funcdemo"></p>




<script>
document.getElementById("myList").addEventListener("change", myFunctiondemo);


function myFunctiondemo() {
  var x = document.getElementById("myList").value;
 document.getElementById("funcdemo").innerHTML = "The selected fruit is: " + x;
}
</script>


</body>
</html>

Output:

JavaScript Dropdown Onchange

NOTE: Internet Explorers and older versions do not support the method of addEventListener().


Related Topics

JavaScript Time Picker Demo

Time Pickers in JavaScript are lightweight and mobile-friendly controls that let the users enter or select date and time values. These values can be from a pop-up calendar or a...

5 minutes read.

Javascript input maxlength

Various attributes often accompany the input tag in HTML to change the properties of the input field. Maxlength() is one such attribute. The maxLength attribute defines the maximum number of characters...

2 minutes read.

Javascript If Else

Introduction The if-else statement is an important control structure for decision-making in code based on some conditions. It gives programmers the ability to add decision-making functionality into their programs. Using if-else,...

6 minutes read.

Javascript Operators

The concept of operators is the basis of nearly all programming languages used in today's computing systems. Operators facilitate the execution of certain functions. For instance, the (+) symbol is...

8 minutes read.

Javascript Find Object In Array

What is find() method? It returns the value of the first element in the given array that fulfils the given testing function. However, if no values from the array are able...

3 minutes read.

Javascript Re-Password Validation

Example <!DOCTYPE html>   <html>   <head>   <script>   function pass_validation()   {   var firstpassword=document.f1.password1.value;     var secondpassword=document.f1.password2.value;     if(firstpassword==secondpassword){     return true;     }     else{   alert("Password does Not Match");     return false;     }     }    </script>   </head>   <body>   <form name="f1" action="/JavaScript/Index" onsubmit="return pass_validation()">   Password:<input type="password" name="password1" /><br/>   Re-enter :<input type="password" name="password2"/><br/>   <input type="submit">   </form>   </body>   </html> Try Now ← Prev Next → ...

1 minute read.

Javascript Data Types

An Overview of JavaScript Data Types In computing, there are a number of data types, including numbers, strings, and booleans, to name a few. The role of these data types in...

5 minutes read.

Minify JavaScript

What do you mean by the term ‘Minification’? The technique of minification reduces the amount of code and markup in your websites and scripting files. It's one of the most used...

4 minutes read.

Javascript Window Object

The window object represents a window in browser. An object of window is created automatically by the browser. Window is the object of browser, it is not the object of JavaScript. The...

3 minutes read.

JavaScript moment date difference

Often date differences are required on the web platforms for various reasons. They can be: To find the duration, of course, someone is pursuingTo find the age of a user from...

7 minutes read.

Javascript Forms Validation

Form validation works at server, after client had entered all the necessary details and then pressed submit button. JavaScript provides the facility to validate the form on the client side...

9 minutes read.

Javascript Dialog Box

A JavaScript dialog box is predefined function which is used to perform different task. Some functions are used in JavaScript Dialog box. FunctionDescriptionalert()To give alert message to userprompt()To input value from...

2 minutes read.

Javascript Variable

Variables are used to store values (name="Ram") or expressions (Sum=x+y). Before using of variable first we need to declare it. We use keyword var to declare a variable like this: var name; There are two types of variables: Local Variable ...

1 minute read.

JavaScript setTimeout()

The setTimeout() method creates a timer that, when it expires, runs a function or provides a piece of code. setTimeout() is an asynchronous function, which means that it will not interrupt...

3 minutes read.

JavaScript redirect URL with parameters

Data can be often passed between web pages as information in URL parameters or query strings. A URL (Uniform Resource Locator) can have a single parameter or multiple parameters.Parameters can...

4 minutes read.

Javascript Event

Events are things that happen, usually user action, that are associated with an object. All object have properties and methods. Some objects also have events. The event handler is a command that is...

1 minute read.

D3.js Tutorial

Introduction of D3.js D3.js is referred to as a JavaScript library that is used to manipulate the documents according to the data. The intensity of D3 is based on web standards...

5 minutes read.

Javascript String

String object works with series of characters. It is used to store and manipulate text. There are two ways to create string in JavaScript: String literal. Using new keyword. String literal By using double quotes...

4 minutes read.

Javascript Print

JavaScript help's the functionality using the print function of window object. In JavaScript print function window.print() print the current web page when it executed. We can call function directly using onclick...

1 minute read.

Javascript comment

Introduction In programming languages, comments are a method to include meaningful annotations within the code that the compiler or interpreter will ignore at run time. That is to say, though comments...

7 minutes read.