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

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -