JavaScirpt Tutorial Index

JavaScript Tutorial Javascript Example Javascript comment Javascript Variable Javascript Data Types Javascript Operators Javascript If Else Javascript Switch Statement Javascript loop JavaScript Function Javascript Object JavaScript Arrays Javascript String Javascript Date Javascript Math Javascript Number Javascript Dialog Box Javascript Window Object Javascript Document Object Javascript Event Javascript Cookies Javascript getElementByID Javascript Forms Validation Javascript Email Validation Javascript Password Validation Javascript Re-Password Validation Javascript Page Redirect Javascript Print

Misc

JavaScript P5.js JavaScript Minify JavaScript redirect URL with parameters Javascript Redirect Button JavaScript Ternary Operator JavaScript radio button checked value JavaScript moment date difference Javascript input maxlength JavaScript focus on input Javascript Find Object In Array JavaScript dropdown onchange JavaScript Console log Multiple variables JavaScript Time Picker Demo JavaScript Image resize before upload Functional Programming in JavaScript JavaScript SetTimeout() JavaScript SetInterval() Callback and Callback Hell in JavaScript Array to String in JavaScript Synchronous and Asynchronous in JavaScript Compare two Arrays in JavaScript Encapsulation in JavaScript File Type Validation while Uploading Using JavaScript or jquery Convert ASCII Code to Character in JavaScript Count Character in string in JavaScript Get First Element of Array in JavaScript How to convert array to set in JavaScript How to get current date and time in JavaScript How to Remove Special Characters from a String in JavaScript Number Validation in JavaScript Remove Substring from String in JavaScript

Interview Questions

JavaScript Interview Questions

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().