codenoob codenoob - 4 months ago 9
Javascript Question

Jquery .on('change' does not detect javascript inputed field

see following snip of code

$(document).on('input','#id1, #id2, #id3', function(){
$id1 = $('#retail').val();
$id2 = $('#tax').val();
$id3 = $('#discount_per_unit').val();
$total = Number($id1) + Number($id2) - Number($id3);
$('#total').val($total);

});


now, user gets to input field for #id1, #id2. but I have a Ajax fill in #id3 for the user depending on conditions. the
.on(input
or
.on(change
event only detect user input but not the Ajax input. So is there a way to get jquery to detect Ajax input as well.

basically I have a drop down menu user can pick, and Ajax will take the drop down value to get an additioanl data and auto fill into id3 input field. look something like this.

$('#id3').val($somevalue);


but my .on("change" does not detect when user picked a different drop down option so the #id3 is updated but my calcuation that uses id1 though id3 did not update unless I change something on id1 or id2.

Answer

You update $('#id3').val($somevalue); didn't trigget any event, they are based on user's actions.

If you want to manually trigger one you can do

$('#id3').val($somevalue).trigger('change') // or 'input'
Comments