jQuery change() with callback function for <select>

Introduction

Write something in the input field, and then press enter or click outside the field.

View in separate window

<!DOCTYPE html>
<html>
<head>
<script 
 src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>//from ww  w.  j a v  a 2s . co m
<script>
$(document).ready(function(){
  $(".field").change(function(){
    $(this).css("background-color", "red");
  });
});
</script>
</head>
<body>

<p>Attach a function to run when an element has been changed:</p>

Enter your name: <input class="field" type="text">
Language:
<select class="field" name="cars">
  <option value="CSS">CSS</option>
  <option value="HTML">HTML</option>
  <option value="Java">Java</option>
  <option value="javascript">Javascript</option>
</select>

<p>Select an option in the drop-down list.</p>

</body>
</html>



PreviousNext

Related