James
James

Reputation: 3805

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

Upvotes: 5

Views: 8353

Answers (4)

MCB
MCB

Reputation: 2073

One option is to track the value outside the scope of your listener

function foo () {
  var value = null;
  $("select#thingy").on("change", function (e) {
    let p = $(this).children("#choose-me").val();
    value = value === p ? null : p;
    $(this).val(value);
  });
}

$(function() {
  foo();
})

Upvotes: 0

Selvakumar Arumugam
Selvakumar Arumugam

Reputation: 79830

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.

Upvotes: 1

Andreas Köberle
Andreas Köberle

Reputation: 110922

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

$("input[name=color]").val('someValue').trigger('change');

Upvotes: 2

ShankarSangoli
ShankarSangoli

Reputation: 69905

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

Upvotes: 12

Related Questions