Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery detecting Programatic change event

If i do jQuery(expr).change( function), then I can get an event function to fire when the user makes a change to the value.

Is it possible to get this to fire if it's changed programatically, ie if I call jQuery(expr).val("moo").

or if some Plain old JavaScript changes it's value?

Thanks for any help.

like image 423
Gunner Avatar asked Dec 11 '08 11:12

Gunner


People also ask

What is a change event in HTML?

The change event occurs when the value of an element has been changed (only works on <input>, <textarea> and <select> elements). The change () method triggers the change event, or attaches a function to run when a change event occurs. Note: For select menus, the change event occurs when an option is selected.

When does the change event occur for selected elements?

For text fields or text areas, the change event occurs when the field loses focus, after the content has been changed. Optional. Specifies the function to run when the change event occurs for the selected elements

How to detect change in a text input box in jQuery?

How to Detect Change in a Text Input Box in jQuery. Answer: Use the input Event. You can bind the input event to an input text box using on() method to detect any change in it. The following example will display the entered value when you type something inside the input field. Example. $("#result").text($(this).val());

How do you trigger a change event?

The change() method triggers the change event, or attaches a function to run when a change event occurs. Note: For select menus, the change event occurs when an option is selected. For text fields or text areas, the change event occurs when the field loses focus, after the content has been changed. Syntax.


2 Answers

After you've changed the value, you can fire the event yourself, and thus calling all the 'onchange' handlers.

jQuery('#element').change();
like image 154
Sander Versluys Avatar answered Sep 20 '22 07:09

Sander Versluys


Changing the value directly in JS doesn't hook into anything which jQuery can listen for to trigger the change event. Like Sander says, you can fire the event handler yourself. If you don't have control over other code modifying these values and that isn't an option, you're kind of out of luck. The only other option which might work would be to have an observer watch the value on an interval using setTimeout, which is really messy and probably not a good idea.

like image 28
Adam Bellaire Avatar answered Sep 21 '22 07:09

Adam Bellaire