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.4.3.change( [eventData], handler(eventObject) )

    eventDataA map of data that will be passed to the event handler.

    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 two variations, and .trigger('change') in the third.

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 you 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. To trigger the event manually, apply .change() without arguments:

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

After this code executes, clicks on Trigger the handler will also alert the message. The message will display 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 bubbles in Internet Explorer, behaving consistently with the event in other modern browsers.

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
});

Support and Contributions

Need help with .change() or have a question about it? Visit the jQuery Forum or the #jquery channel on irc.freenode.net.

Think you've discovered a jQuery bug related to .change()? Report it to the jQuery core team.

Found a problem with this documentation? Report it to the jQuery API team.

* All fields are required
  • Anonymous

    Seems like this would eliminate the need for a plugin like dirtyForm?

  • Josh

    The demo above is broken. I don’t know why it doesn’t work.

  • http://www.learningjquery.com/ Karl Swedberg

    Could you please provide more information about how it’s broken and in which browser(s)?

    Thanks.

  • Anonymous

    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.

  • http://www.wishlost.com/ sanjitn

    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!

  • http://www.learningjquery.com/ Karl Swedberg

    Thanks for the details. I definitely see what you’re talking about in IE8 with multiple selection using arrow keys. Not sure what can be done about it or if it’s a jQuery bug or an IE issue. In any case, I’d say it’s definitely worth posting this to the jQuery forum. http://forum.jquery.com/

    Thanks.

  • Anonymous

    this event not work with input type=file in IE

  • Anonymous

    Is there a way to detect the change inmediately, instead after losing focus?

  • Dale

    Just posting an example on how to stack element id’s to run a snippet of any element id changes:

    $(“#id1,#id2″).change(function() { //if the value of these elements change, do this
    var id1 = $(“#id1″).val();
    var id2 = $(“#id2″).val();
    $.get(“checkID.php”, { id1: id1, id2: id2 }, function(data) {//Use GET to run external script, passing on vars as appropriate.
    $(“#elementCheck”).html(data); //Return the output to the appropriate element.
    });
    return false;
    });

    the same should apply for blur() etc.

  • http://twitter.com/JoeyD473 JoeyD473

    Using the arrow keys to change the selected option on a select element doesn’t seem to activate the change event until focus taken away from the element.

  • Dale

    Unfortunately, there is no way (that i know of) to activate a change in any element until focus is taken away for the element.

  • Bo

    Well, you could use a timer that watches for changes in the value of an element and fires an “event” when the change is detected. Also, for the arrow keys on the selected option you could just use the onkeydown/onkeyup events.

  • http://twitter.com/supermad79 Markus Diesing

    Adding a handler for the keyevents could look like this:

    function doChange(){
    alert(‘do something’);
    }
    $(“select”).keypress(function (){
    doChange();
    });

    $(“select”).change(function (){
    doChange();
    });

  • pierre

    on ie 7 the demo doesn’t work.
    The text change after you press another key or click somewhere if you use the keyboard to select element.

  • Sam

    Markus Diesing’s code seems to do the trick for triggering change() without losing focus. Works for me on IE and FF and all the other browsers:

    $(“select”).keypress(function (){
    doChange();
    });

    $(“select”).change(function (){
    doChange();
    });

  • cegiac

    Actually the change event with select worked fine in IE with jQuery 1.3.2. It’s been broken since.

  • David

    Did any get an answer for this ? On FF the code works fine but on IE6 the change is not detected immediately.

  • http://peterduerden.tumblr.com/ Peter Duerden

    Testing the above documentation Demo I get the following results:

    IE6: div is only updated after a subsequent selection or when list loses focus, e.g., initial load displays “Candy Caramel” but after clicking the “Chocolate” option the div text is only updated to display “Chocolate” after either clicking another item or the list losing focus (click elsewhere on page or tab.)

    IE7/8: changes to the selected list items are displayed immediately in the div.

    My problem however is specific to checkboxes: . For this element the change() event is only fired after losing focus in IE6, 7 and 8. I have switched to using the click() event for my checkboxes.

  • Karlsson

    for me it’s not working in the demo above.
    using IE8 and changing value with keyboard… the value selected is not the same as the value printed.

  • SmittyDesign

    for me to get this to work, I had to add the change method inside a ready method.

    Example: jQuery(“#my_select”).ready( function(){ jQuery(“#my_select”).change( function(){ alert('change fired'); } ) } );

    You can also add it to a jQuery(document).ready(); instead of the specific element.

    This is what got it working on our end.

  • Anonymous

    This is similar to how I did it, although you should use keyup instead of keypress: For example, if you are calculating the value with three or an and a — #total using the values of #subtotal and #quantity then you want the price of #total to be updated as soon as you have typed in an extra character into the subtotal field, not only if you click out of it or add an additional character after it.

  • http://niyum.com Sitthykun

    Or

    $(‘select’).bind({
    keypress: function() {
    doChange();
    },
    change: function() {
    doChange();
    }
    });

  • http://www.learningjquery.com/ Karl Swedberg

    Can you please ask your question on the forum?

    thanks.

  • Kawa

    Tried to use it to detect change when user selects file in dialog box, but unfortunately it doesn't work in IE8. The event triggers only when input loses focus.

  • http://twitter.com/MichaelPStone Michael Stone

    This actually doesn’t work too well in IE. Whether it’s IE6 or IE8, it’s not as fluent as it is in Firefox. If you’re having issues with this in IE, then I’d suggest just using the .click().

  • Alain

    I had the same problem and solved it using click instead of change in combination with a focus. Maybe this helps with change, too. It seems to be the same problem.

    $(select).live(“click”, function() {
    $(this).focus(); //IE 6 hack

    //Here goes your action

    });

  • theBaron

    how could you place a filter on the script, ie. only show the div if ‘Cookie’ is selected?

  • http://www.learningjquery.com/ Karl Swedberg

    $(‘select’).change(function() {
    if ( $(this).val() == ‘Cookie’ ) {
    // do something
    }
    });

  • BMan

    To get change() to work in IE, the workaround is:
    $(element).change(function() { doChange(); }).attr(“onchange”, function() { doChange(); });
    Remember that the doChange() function should not be using $(this).

  • ordiminnie

    Or the .blur() which is closer to .change() than .click()

  • Markus

    Alternatively, couldn’t you do this?

    $(element).change(function() {
    //stuff
    }).attr(“onchange”,function() {$(this).change()});

  • bufo

    If a form has pre-filled values, then if a user clicks thru an input box without changing the existing value the change() event is triggered. Why is this happening and what can be done to fix this.

  • RetireIE

    I can't get “change” or “delegate” to work in IE. Maybe I have misunderstood your workaround. How would you script the following :
    $(“.foo”).live(“change”, function() {
    do something;
    });
    Thanks for helping! IE makes me age too fast :(

  • http://kflorence.myopenid.com/ Kyle Florence

    Using “propertychange” instead of “change” seems to work in IE.

    $(':checkbox')).bind($.browser.msie ? 'propertychange': 'change', function(e) {
    // stuff
    });

  • Frank Pietersen

    After updating to jQuery 1.4.2 it works like a charm in IE..

  • iGanja

    yes, this seems to be fixed in 1.4.2

  • Persevere4triumph

    Does .change() predominantly work on a text input when it blurs or is focused into? I.e. can a change occur when its text value changes as well, like each time a user presses a button?

  • http://pulse.yahoo.com/_RPVHR734DVN4TPQKDROKDPXL4E Dario Zadro

    I'm curious how to get this working also. Someone, please respond :)
    It seems like I have to hit “enter” for the .change to work…misleading, no?

    It appears that using .delegate with “change” is the same thing. I tried using “keypress”, but my browser is too quick to accept it. Ideas?

  • http://www.learningjquery.com/ Karl Swedberg

    No. For that you should use .keyup().

  • ben adler

    Same thing using seclectboxes in IE6.

    // Changing selection with scrollwheel or arrowkeys doesn't trigger event
    $('#presets').change(function(e){…

    // Works just fine:
    $('#presets').bind($.browser.msie ? 'propertychange': 'change', function(e) {…

    Thank you Kyle!

  • http://pulse.yahoo.com/_AW6HRPCON7UJPHWPCJQCAKBBF4 yahoo-AW6HRPCON7UJPHWPCJQCAKBBF4

    IE8 triggers 'mousedown' even faster than 'click' or 'change'.

  • Aa

    sdgsgd
    sdgsdg

  • Jy1337

    For multiple <select> elements, in IE and Chrome (maybe others), if you click an item and release the mouse outside of the </select>, .change() will not be triggered immediately, only after you click something else on the page.

    This is a problem when the user is expected to make a multiple selection by clicking an item and then dragging the mouse on other items until the selection is made. Quite often the user will release the mouse outside of the <select>.</select>

  • qarluq

    Hi,
    I have two checkbox elements. I want to get the text value (“Sonday”, “Monday”) of these checkbox . And I want to change them with some other values (“tuesday”, Wednesday”). I do not know how to get these two values first. I tried .val() and .text() methods, they did not work. Do you any idea about how to get them and replace with some other values?
    <form>

    <input class=”checkbox” id=”1″ name=”attribute” type=”checkbox” value=”one”>Sunday

    <input class=”checkbox” id=”2″ name=”attribute” type=”checkbox” value=”two “>Monday

    </form>

  • Robin

    In older, pre-HTML5 browsers, “keyup” is definitely what you're looking for.

    In HTML5 there is a new event, “input”, which behaves exactly like you seem to think “change” should have behaved – in that it fires as soon as a key is pressed to enter information into a form.

    [code]$('element').bind('input',function);[/code]

  • Itanex

    First you cant have IDs that start with numbers.
    <input id=”one” name=”weekdays” type=”checkbox” value=”one”> Sunday
    <input id=”two” name=”weekdays” type=”checkbox” value=”two”> Monday

    Now you need to select the checkboxes:
    var checkboxes = $(“checkbox[name='weekdays']“);

    Now if you want to output the list when clicked on:

    function report(checkboxlist){
    var returnstr = “”;
    var len = checkboxlist.length;
    for(var i = 0; i < len; ++i){
    returnstr = returnstr + checkboxlist[i].val();
    }
    return returnstr;
    }

    Now that you have a method lets wire it up:
    checkboxes.bind(“click”, { c: checkboxes }, function(e){
    console.log(report(e.c));
    });

    You will need to format and mold this but this gives you the idea.

  • Tuan

    For text input, it works only when the input has focus and blur. In my case when i have the textfield to be updated automatically so it never has the focus so the .change event never gets fired when value is changed.

    How could we get this over?

  • Mauro

    Event input is great but in jquery api there is no documentation about that event.
    Why?

  • Zgfjlzm

    Is very good!!

  • Zgfjlzm

    Is very good!!

  • Fake Truth

    How to detect changing of a text field when user inserts a text from the clipboard using mouse (trackpad)? 'change', 'keyup', 'click' events are not fired in this case.
    How it is done on Twiiter?

  • http://twitter.com/lukecarbis Luke Carbis

    I have the same problem – how do I detect a change event when a user is using a dictate program to input text into a textarea? The change, keyup and click events aren't fired.

  • Anonymous

    try onpaste
    not documented byt works

  • ucupumar

    jquery rockss!!

  • Cao

    how can i do convert from array text and trim it in to string t valid ?
    Help plz

  • Rutger

    You can use #textfield.live('change', function(event) { });

    That will run when the textfield is left (ex when pressing the enter key or clicking somewhere else)

  • mitt

    The change event doesn't fire on radio button when another radio button form this group is clicked, thus the value of first one is in fact changed. Is there any know workaround?

  • Raximus

    So, your'e saying that when using radio buttons, the first one just changes when you click a new one? That's about the concept of radio buttons. Use checkboxes if you want more than one to be tickable.