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.

1
  • 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 to set up a custom listener Commented Dec 5, 2017 at 20:15

3 Answers 3

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/

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

0

Add the one more line after your code:

$("input").val(currentColorChosen); $("input").bind("change"); 

7 Comments

This will depend on your jQuery lib version
Currently using the latest version, does this only work for an older version?
It can be like $("input").on("change"); or $("input").change();
.bind('change') will do nothing. Maybe you meant .trigger('change')
Nope, I didn't mean that. It works for sure. But for this question, I would use $("input").on("change"); as question has it with .on()
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.