jquery - Onchange of a input field changed by Javascript - Stack Overflow

I have a input field type="hidden" which is changed by javascript itself.When this field chan

I have a input field type="hidden" which is changed by javascript itself.

When this field changes i want an event/function to be triggered.

Tried this:

$(".product_id").on("change paste keyup input", function(event){alert('1');});
$(".product_id").blur(function(event){alert('1');});
$(".product_id").keypress(function(event){alert('1');});

But it does not seem to work as the value is changed by other JavaScript file itself.

I cannot change or add to the existing JavaScript. I cannot add anything to the input field.

I have a input field type="hidden" which is changed by javascript itself.

When this field changes i want an event/function to be triggered.

Tried this:

$(".product_id").on("change paste keyup input", function(event){alert('1');});
$(".product_id").blur(function(event){alert('1');});
$(".product_id").keypress(function(event){alert('1');});

But it does not seem to work as the value is changed by other JavaScript file itself.

I cannot change or add to the existing JavaScript. I cannot add anything to the input field.

Share Improve this question asked Dec 26, 2016 at 13:53 maharshimaharshi 59412 silver badges33 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 6

You can use jQuery's trigger method & change event.

Changes in value to hidden elements don't automatically fire the .change() event. So, as soon as you change the hidden inputs, you should tell jQuery to trigger it using .trigger('change') method.

You can do it like this:

$(function() {
  $("#field").on('change', function(e) {
  	alert('hidden field changed!');
  });
  
  $("#btn").on('click', function(e) {
  	$("#field").val('hello!').trigger('change');
    console.log('Hidden Filed Value: ' + $('#field').val());
  });
  
  console.log('Hidden Filed Value: ' + $('#field').val());
})
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="field" type="hidden" name="name">
<button id="btn">
  Change Hidden Field Value
</button>

Hope this helps!

It's just a hack if you can't trigger change event from sources.

May not be a good solution with setInterval(). But helps in the cases where you can't trigger change event from sources which are changing the hidden input value.

$("#input")[0].oninput = function(){
  $("#hidden").val($(this).val()); //setting hidden value
}
var oldVal = $("#hidden").val();
setInterval(function(){ //listening for changes
  var newVal = $("#hidden").val(); 
  if(newVal !== oldVal){
    console.log(newVal);
    oldVal = newVal;
  }
}, 1000);
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="hidden" id="hidden">

<input type="text" id="input">

var element = $(".product_id");
element.on("change", function(){
    alert('hey');
}).triggerHandler('change');

and we need to trigger it programmatically like:

$('.product_id').val('abcd').triggerHandler('change');

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745348059a4623656.html

相关推荐

  • jquery - Onchange of a input field changed by Javascript - Stack Overflow

    I have a input field type="hidden" which is changed by javascript itself.When this field chan

    7小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信