Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery textbox.val('xxxx') not causing change to fire?

I have the following jQuery code in place on my page:

var isChanged = false;
$(document).ready(function()
{
    $('.change').change(function() {
        isChanged = true;
    });
});

I am using a plugic that modifies the value of the text box it is linked to using:

target.val('xxxx');

the text box in the html (from asp.net) is:

<input name="ctl00$cphHolder1$rptFlex$ctl01$txtLeftRank"
  type="text"
  value="52°"
  id="ctl00_cphHolder1_rptFlex_ctl01_txtLeftRank"
  class="change atiselector" />

When the value of the text box is changed using code, the change is not firing. If I type in the text box, the change fires. What am I missing?

like image 239
DC. Avatar asked Feb 11 '10 20:02

DC.


3 Answers

That's the way it works. If you need the change of value to trigger the "change" event, you can explicitly do so by:

$('input#whatever').val('hi').change();
like image 168
Pointy Avatar answered Nov 16 '22 14:11

Pointy


$('.change').change() will fire the event. Just changing the attributes doesn't fire the event.

like image 4
Yuriy Faktorovich Avatar answered Nov 16 '22 16:11

Yuriy Faktorovich


According to DOM Level 2 Event Specification:

The change event occurs when a control loses the input focus and its value has been modified since gaining focus.

That means that change event is designed to fire on change by user interaction. Programmatic changes doesn't cause this event to be fired.

like image 4
oak Avatar answered Nov 16 '22 15:11

oak