javascript - How to trigger JS native even addEventListener("change",function) by JQuery -
this not duplicate of how trigger jquery change event in code, since deals interaction of jquery jquery event listeners, while question discusses interaction of jquery native event listeners.
i use addeventlistener bind change event input, $('#input').val('bbbb').change(); can't trigger alert, how trigger addeventlistener("change",function) function jquery
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>code</title> </head> <body> <script src="http://o6flfbzth.bkt.clouddn.com/jquery-1.12.0.min.js"></script> <input id="input" type="text" value="input" /> <script type="text/javascript"> document.getelementbyid("input").addeventlistener("change", function() { alert(this.value); }); </script> <input type="button" value="change input" onclick=" $('#input').val('bbbb').change();" /> </body> </html>
you cannot trigger native event jquery's .trigger('change')
. need create native event , dispatch it:
var evt = document.createevent('htmlevents'); evt.initevent('change', true, true); // ^ ^ ^ // | | cancelable // | bubbles // type
and fire it:
var el = document.getelementbyid("input"); el.dispatchevent(evt);
Comments
Post a Comment