javascript - How to bind dynamic content to document-ready events? -


i not amazing jquery or javascript matter, there duplicate question on this, not sure how phrase search. think have use .trigger() or on() not sure how implement if so, or perhaps it's not possible in scenario.

i have 3 pages, index.php page html, dispatch.php page serves dynamic content index page via ajax, , lastly scripts.js jquery/javascript.

here psuedo-code example:

/index.php

<form id="form1">     <input type="text" name="whatever" />     <input type="submit" name="whatever_submit" disabled="disabled" class="enable" /> </form> <form id="form2">     <input type="text" name="whatever2" />     <input type="submit" name="whatever_submit2" disabled="disabled" class="enable" /> </form> <!-- dispatch page deliver content --> <div id="drophere"></div> 

/js/scripts.js

$(document).ready(function() {     // runs on page load , enables submit buttons     $('.enable').attr('disabled',false);     // code dispatch     $.ajax({         url: '/dispatch.php',         ....ect }); 

/dispatch.php

<!-- php stuff, sends additional form --> <form id="form3">     <input type="text" name="whatever3" />     <input type="submit" name="whatever_submit3" disabled="disabled" class="enable" /> </form> 

the issue here jquery $('.enable').attr('disabled',false); works on form1 , form2 using ajax load form , doesn't bind dynamic form3 because other 2 effected on document-ready. use on.() interactive elements because there trigger (click, mouseover, etc) it's passive application don't know how (maybe there no way or maybe not calling right event type?). workaround, put same enabling script dispatched html:

<form id="form3">     <input type="text" name="whatever3" />     <input type="submit" name="whatever_submit3" disabled="disabled" class="enable" /> </form> <!-- added @ bottom , runs content loads index.php page --> <script> $('.enable').attr('disabled',false); </script> 

this seems clumsy workaround, there proper way bind dynamic content document-ready event javascript located on scripts.js or proper way redo piece of code work on click event using on() similar to:

$(document).on('click','.enable',function(){     $(this).attr('disabled',false);     // possibly form submit here...whatever work }); 

i hoping there way bind because have lot of other similar instances apply to, thinking may have switch per-click basis? assistance appreciated.

just execute $('.enable').attr('disabled',false); in callback function of ajax-call.

what using mutationobserver part of modern browser js engines: https://developer.mozilla.org/en/docs/web/api/mutationobserver


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -