jQuery change()

The jQuery change() method is used to attach a function to run when a change event occurs i.e, when the value of an element is changed. This method is limited for form fields only, for input elements, textarea boxes and select elements.

  • The change event is fired immediately in case of select boxes, checkboxes, and radio buttons, as soon as the user makes a selection.
  • For elements of other types, the change event occurs as soon as the field loses focus.

Syntax:

To trigger the change event for selected elements.

$(selector).change() 

To add a function to the change event.

$(selector).change(function)  

Function:

  • It is an optional parameter.
  • The function parameter specifies the function to run when the event occurs.

 

Example 1




  
    
      
    
    
     

Try it

JS Bin on jsbin.com

Example 2




  
    
      
    
    
  

Try it

JS Bin on jsbin.com