Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery input field change even when value is changed via jquery

I am attempting to capture when a text input field's value is changed. I am using:

$("input[name=color]").change(function(){

This works if I type the new value in directly. However, the value of the input field is being changed by jquery. There are several of these fields, and I need to know when any have changed. Is there a way to detect this change?

Sorry, I wasn't clear. I'm not the one changing the value. It's an addon that I would rather not modify in case I need to port it to another project.

WORK-AROUND

Ok, so you can't do what I wanted to do, but here is a work-around. I just made an interval and changed my change event to an each event.

setInterval(function(){
    $("input[name=color]").each(function(){ my code })
},100);
like image 756
James Avatar asked Feb 09 '12 21:02

James


3 Answers

There is no way to detect the changes done programmatically.

However you can trigger a change event whenever you modify its value.

$("input[name=color]").val("newValue").change();
like image 115
ShankarSangoli Avatar answered Sep 21 '22 00:09

ShankarSangoli


When change the value by jquery you can fire the change event with jquery afterwards

$("input[name=color]").val('someValue').trigger('change');
like image 41
Andreas Köberle Avatar answered Sep 20 '22 00:09

Andreas Köberle


No, onchange event will not be triggered when the value is changed using javascript. Alternatively you can call the onchange event when you change the value.

like image 36
Selvakumar Arumugam Avatar answered Sep 18 '22 00:09

Selvakumar Arumugam