Javascript Ajax jQuery Html PHP Example Quiz New MORE

jQuery change() Method

jQuery change event occurs when the value of an element is changed. Generall it is used in HTML select box. It is an inbuilt function of jquery.

Syntax:

$(selector).change() It triggers the change event for selected elements. $(selector).change(function) It adds a function to the change event.

Example:

	<!DOCTYPE html>
	<html>
	<head>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js">
	</script>
	<script>
	count = 0;
	$(document).ready(function(){
	  $("select").change(function(){
		var data=$("select option:selected").val();
		if(data!=""){
		   $("#message").text(data);
		}
		else{
		   $("#message").text("Nothing");
		}
	  });
	});
	</script>
	</head>
	<body>

		<select>
		  <option value="">Select</option>
		  <option value="Cricket">Cricket</option>
		  <option value="Football">Football</option>
		  <option value="Hockey">Hockey</option>
		</select>
		<p>You Select: <span id="message"></span></p>

	</body>
	</html>
Run it Yourself »