Javascript Onchange Event Example

Apr 06, 2021 . Admin

Hello Friends,

Now let's see example of how to use javascript onchange event example. We will use how to use if onchange event in javascript. Here you will learn how to use javascript onchange event. This is a short guide on onchange event. Let's get started with how to onchange event in javascript.

Here i will give you many example how you can check javascript onchange event.

Example: 1
In this example i will use id for onclick event
<!DOCTYPE html>
<html>
<head>
   <title>Javascript - How to check if onchange event?-MyWebtuts.com</title>
</head>
<body>

	<label>Enter your text:</label> <input type="text" id="text">

	<p>When you leave the input field, a function is triggered which transforms the input text to upper case.</p>

	<script>
	
		document.getElementById("text").onchange = function() {myFunction()};

		function myFunction() {
		    var x = document.getElementById("text");
		    x.value = x.value.toUpperCase();
		}

	</script>

</body>
</html>
Output: 1
    JAVASCRIPT
Example: 2 In this example i will use attribute for onclick event.
<!DOCTYPE html>
<html>
<head>
   <title>Javascript - How to check if onchange event?-MyWebtuts.com</title>
</head>
<body>
	<label>Enter your text:</label> <input type="text" name="txt" value="javascript" onchange="myFunction(this.value)">

	<script>

		function myFunction(val) {
		    alert("The input value has changed. The new value is: " + val);
		}

	</script>

</body>
</html>
Output: 2
    The input value has changed. The new value is: javascript1

It will help you....

#Javascript