Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

jQuery change()


jQuery change event occurs when the value of an element is changed. It works only on form fields. When the change event occurs, the change () method attaches a function with it to run.

Note: This event is limited to <input> elements, <textarea> boxes and <select> elements.

  • For select boxes, checkboxes, and radio buttons: The event is fired immediately when the user makes a selection with the mouse.
  • For the other element types: The event is occurred when the field loses focus.

Syntax:

  1. $(selector).change()

It triggers the change event for selected elements.

  1. $(selector).change(function)

It adds a function to the change event.

Parameters of jQuery change() event

Parameter Description
Function It is an optional parameter. It is used to specify the function to run when the change event occurs for the selected elements.

Example of jQuery change() event

Let’s take an example to demonstrate jQuery change() event.

  1. <!DOCTYPE html>
  2. <html lang=“en”>
  3. <head>
  4.   <meta charset=“utf-8”>
  5.   <title>change demo</title>
  6.   <style>
  7.   div {
  8.     color: red;
  9.   }
  10.   </style>
  11.   <script src=“https://code.jquery.com/jquery-1.10.2.js”></script>
  12. </head>
  13. <body>
  14.  <select id=“se” name=“actors” >
  15.   <option>Uthappa</option>
  16.   <option selected=“selected”>Kattapa</option>
  17.   <option>Veerappa</option>
  18.   <option>Bahubali</option>
  19.   <option>Bhallal Dev</option>
  20.   <option>Awantika</option>
  21. </select>
  22. <div id=“loc”></div>
  23.  <script>
  24. $( “select” ) .change(function () {
  25. document.getElementById(“loc”).innerHTML=“You selected: “+document.getElementById(“se”).value;
  26. });
  27. </script>
  28.  </body>
  29. </html>

Test it Now

Output:

Let’s see another example of jQuery change event where we are providing option to select multiple data using ctrl key.

  1. <!DOCTYPE html>
  2. <html lang=“en”>
  3. <head>
  4.   <meta charset=“utf-8”>
  5.   <title>change demo</title>
  6.   <style>
  7.   div {
  8.     color: red;
  9.   }
  10.   </style>
  11.   <script src=“https://code.jquery.com/jquery-1.10.2.js”></script>
  12. </head>
  13. <body>
  14.  <select name=“Employees” multiple=“multiple”>
  15.   <option>Uthappa</option>
  16.   <option selected=“selected”>Kattapa</option>
  17.   <option>Veerappa</option>
  18.   <option selected=“selected”>Bahubali</option>
  19.   <option>Bhallal Dev</option>
  20.   <option>Awantika</option>
  21. </select>
  22. <div></div>
  23.  <script>
  24. $( “select” )
  25.   .change(function () {
  26.     var str = “”;
  27.     $( “select option:selected” ).each(function() {
  28.       str += $( this ).text() + ” “;
  29.     });
  30.     $( “div” ).text( str );
  31.   })
  32.   .change();
  33. </script>
  34.  </body>
  35. </html>

Comments

comments