×

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 loops

Loops in JavaScript: Loops are used to execute a specific piece or block of code repeatedly until the given condition is satisfied. What is a Loop? Almost all loops contain a certain...

7 minutes read.

Javascript Password Validation

Here we see that how to validate any password field. Like password field can be blank and length of password is minimum 8 characters. Example <!DOCTYPE html>   <html>   <head>   <script>   functionpass_validation()   {     var password=document.myform.password.value;     if (password==null || password=="")   {     alert("password can't be blank");     return false;     }   else if(password.length<8)   {     alert("Password must be at least 8 characters long.");     return false;       }     }     </script>   </head>   <body>   <form name="myform" method="post" action="register.php" onsubmit="return pass_validation()" >   Password: <input type="password" name="password">   <input type="submit" value="submit">   </form>   </body>   </html> Try Now ← Prev Next → ...

1 minute 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 Page Redirect

The window.location object can be used to get the current page address (URL) and to redirect browser to new page. In some websites when we visit, we face a situation where we...

3 minutes read.

JavaScript Console log Multiple variables

What does console.log() do in JavaScript? The console.log() is a function in JavaScript language. It is used to: Print any message in the console which is visible to the userPrint the values...

3 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.

Array to String in JavaScript

As a web developer, you often have to deal with multiple data types in a single application. How to convert an array to a string in JavaScript is essential for...

19 minutes read.

JavaScript Image resize before upload

Image resizing can be a quite tedious task, so it is usually done on the server-side. The resized image file is then delivered to the client side. However, sometimes the...

3 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.

Synchronous and Asynchronous in JavaScript

JavaScript is a powerful and versatile language that is used all over the web, from small websites to large applications. It is essential for developers to understand the differences between...

9 minutes read.

Javascript Example

What is JavaScript? JavaScript is a highly versatile programming language. It provides the capability to add interactivity to static web pages, which are designed with HTML and CSS. It also provides...

5 minutes read.

Javascript Switch Statement

Introduction Conditional statements are simple building blocks in programming languages and are utilized fairly frequently. This portion of Developing Conditional Statements in JavaScript talks about how to utilize the if, else,...

5 minutes read.

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 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 Arrays

JavaScript Arrays: JavaScript Arrays are a type of variables that allows us to store the individual or the group of values under a single variable. What is an Array in JavaScript? The array...

6 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.

Functional Programming in JavaScript

Most developers work with object-oriented programming while developing their applications, but sometimes they need to change their approach to solve some specific tasks. Although mostly we tend to go for...

6 minutes read.

Javascript Document Object

Document object represents the whole HTML documents. When HTML document is loaded in browser it becomes the document object. It is the root elements that represent the HTML documents. With the help of...

2 minutes read.

Javascript getElementByID

The document.getElementById() method returns the element of specified id. In below example we receive input field by using document.getElementById() method, here document.getElementById() receive input value on the basis of input field...

1 minute read.

What is Ternary Operator in JavaScript?

In some cases, a ternary operator can be used instead of an if-else expression. A ternary operator examines a condition and then runs a block of code in response to...

4 minutes read.