HTML event attribute onchange








The onchange attribute event is triggered when the value of the element is changed.

What's new in HTML5

None.

Syntax

<element onchange="script or Javascript function name">

Supported Tags

<input type="checkbox">, 
<input type="file">, 
<input type="password">, 
<input type="radio">, 
<input type="range">, 
<input type="search">, 
<input type="text">, 
<keygen>, 
<select> 
<textarea>




Browser compatibility

onchange Yes Yes Yes Yes Yes

Example

<!DOCTYPE html>
<html>
<body>
<!--  ww  w.  j a  v  a2 s . co m-->
<select id="mySelect" onchange="myFunction()">
  <option value="CSS">CSS</option>
  <option value="HTML">HTML</option>
  <option value="Javascript">Javascript</option>
  <option value="SQL">SQL</option>
</select>

<script>
function myFunction() {
    alert(document.getElementById("mySelect").value);
}
</script>

</body>
</html>

Click to view the demo





Example 2

<!DOCTYPE html>
<html>
<body>
<!--   w ww .  j a  va  2  s . c  o  m-->
Enter some text: 
<input type="text" name="txt" value="Hello" onchange="myFunction(this.value)">
<input type="text" name="txt2" value="Hello another" onchange="myFunction(this.value)">
<script>
function myFunction(val) {
    alert(val);
}
</script>
<p>Edit text and tab out.</p>
</body>
</html>

Click to view the demo