1

I am currently using this function to detect for any changes made to an input box in jQuery.

$(document).on("input change paste keyup", "input, textarea, select", function(e) {
    //do something
});

The problem is that when you change the value of an input through code such as this $("input").val(currentColorChosen);, the function does not run.

How do I solve this so that the function can detect all changes made to an input, including value changes made through jQuery.

Osman
  • 165
  • 1
  • 10
  • 1
    direct changes to the value attribute of inputs do not by default trigger any events; you'll probably need to use something like [`MutationObserver`](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver) to set up a custom listener – Hamms Dec 05 '17 at 20:15

3 Answers3

2

$("input").change(function(){console.log("there was a change")}) and I think you can pass the value into it as well, it's a shortcut for .on( "change", handler )

More info: https://api.jquery.com/change/

1

I had just posted answer here

You can try using interval or timeout like this

var searchValue = $('#myTextbox').val();

function checkSearchChanged() {
    var currentValue = $('#myTextbox').val();
    if ((currentValue) && currentValue != searchValue && currentValue != '') {
        searchValue = $('#myTextbox').val();
       console.log(searchValue)
    }
    setTimeout(checkSearchChanged, 0.1);
}

$(function () {
    setTimeout(checkSearchChanged, 0.1);
});

checkout working plunker here

0

Add the one more line after your code:

$("input").val(currentColorChosen);
$("input").bind("change");
slon
  • 1,002
  • 1
  • 8
  • 12