17

When we use the method jQuery.Val( value ) over a DOM element to change its value.

Shouldn't the element dispatch an event informing that its value has changed? -I though the event 'change' was going to be dispatched.

If it shouldn't why?

Live Demo

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
SDReyes
  • 9,798
  • 16
  • 53
  • 92

1 Answers1

35

It's not dispatched, this just doesn't happen when it's programmatically changed. You are however free to fire the event when needed like this:

$('#Anne').val('Jenny').change();

You can test it here.

The reasoning? If the user changed something you may need to react, but if you changed something then you know what happened, and firing an event handler may be completely wasteful...so it's up to you to call that event handler if it's needed.

Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
  • 4
    @SDReyes: Also: If event handlers would fire automatically for progammatic changes, this could trigger an unavoidable infinite loop, depending on what you do. It's good that they don't. – Tomalak Oct 18 '10 at 17:20