jQuery API

.change()

.change( handler(eventObject) ) Returns: jQuery

Description: Bind an event handler to the "change" JavaScript event, or trigger that event on an element.

  • version added: 1.0.change( handler(eventObject) )

    handler(eventObject)A function to execute each time the event is triggered.

  • version added: 1.0.change()

This method is a shortcut for .bind('change', handler) in the first variation, and .trigger('change') in the second.

The change event is sent to an element when its value changes. 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, but for the other element types the event is deferred until the element loses focus.

For example, consider the HTML:

<form>
  <input class="target" type="text" value="Field 1" />
  <select class="target">
    <option value="option1" selected="selected">Option 1</option>
    <option value="option2">Option 2</option>
  </select>
</form>
<div id="other">
  Trigger the handler
</div>

The event handler can be bound to the text input and the select box:

$('.target').change(function() {
  alert('Handler for .change() called.');
});

Now when the second option is selected from the dropdown, the alert is displayed. It is also displayed if we change the text in the field and then click away. If the field loses focus without the contents having changed, though, the event is not triggered. We can trigger the event manually when another element is clicked:

$('#other').click(function() {
  $('.target').change();
});

After this code executes, clicks on Trigger the handler will also alert the message. The message will be displayed twice, because the handler has been bound to the change event on both of the form elements.

As of jQuery 1.4 the change event now bubbles, and works identically to all other browsers, in Internet Explorer.

Examples:

Example: Attaches a change event to the select that gets the text for each selected option and writes them in the div. It then triggers the event for the initial text draw.

<!DOCTYPE html>
<html>
<head>
  <style>

  div { color:red; }
  </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
	<select name="sweets" multiple="multiple">
    <option>Chocolate</option>
    <option selected="selected">Candy</option>

    <option>Taffy</option>
    <option selected="selected">Caramel</option>
    <option>Fudge</option>
    <option>Cookie</option>

  </select>
  <div></div>
<script>
    $("select").change(function () {
          var str = "";
          $("select option:selected").each(function () {
                str += $(this).text() + " ";
              });
          $("div").text(str);
        })
        .change();
</script>
</body>
</html>

Demo:

Example: To add a validity test to all text input elements:

$("input[type='text']").change( function() {
  // check input ($(this).val()) for validity here
});

Comments

  • Support requests, bug reports, and off-topic comments will be deleted without warning.

  • Please do post corrections or additional examples for .change() below. We aim to quickly move corrections into the documentation.
  • If you need help, post at the forums or in the #jquery IRC channel.
  • Report bugs on the bug tracker or the jQuery Forum.
  • Discussions about the API specifically should be addressed in the Developing jQuery Core forum.
  • cryx
    How do I use the last example but with displaying the values instead? I've tried replacing the "text" with "val" instead and removing "str" but it doesn't work.
  • Xiaolei Jin
    Hi, this function doesn't works when it's used for an input file type in IE until you click somewhere after select the file...
  • Bob
    I don't suppose it's possible for the change event to be triggered if a select box's value changes via the keyboard, while the select box retains keyboard focus, is there? As far as I can tell, the change event only triggers after the select box loses keyboard focus.
  • Josh
    The demo above is broken. I don't know why it doesn't work.
  • Could you please provide more information about how it's broken and in which browser(s)?

    Thanks.
  • Hi, Karl.
    Can you try the same example in IE 8? When using the keyboard to change the selected item the item shows the previous selected item & only shows the current selected on the next key press. There is no such issue under Mozilla Firefox 3.6.
    Thank you!
  • mgdelmonte
    Moving the cursor with the arrow keys either doesn't change the text or changes it to the previously (not currently) selected item.

    Also change() is broken for textareas. It does not register on the first load of a page. It will only register after the text has been edited once, then edited again.
  • pwc1011
    Seems like this would eliminate the need for a plugin like dirtyForm?