jquery - Execute .on() function automatically after page load -


i use $("#container").on("click", contenteffects); make jquery code work again after ajax call. however, hope .on() function can executed , automatically after page load, , without click event. possible?

please take @ site , javascript file:

http://peters-playground.com/blog.html

https://github.com/p233/p233.github.com/blob/master/js/script.js#l30-l78

when new post loaded ajax, have click on post content enable javascript , need click button twice make work. annoying. how can avoid problem?

thank you!

you can run in chain:

// anonymouse function passed $ invoked // when document has loaded $(function () {      // after binding click handler     // invoke click event on #container     $("#container").on("click", contenteffects).click();  }); 

what sounds should consider though event-delegation, , binding events nearest static ancestor element present when page loads. instance, suppose have list of items populated dynamically ajax:

<ul id="items">     <li>first item on load.</li>     <!-- more items loaded          via ajax later on ---> </ul> 

you might want list items when click on them, bind handler click event:

$("#items li").on("click", function () {     alert( $(this).html() ); }); 

this works list items already on page, experiencing, new items won't have bound them, after ajax have re-bind new list items.

instead of doing this, can bind ul element, present, , listen events originated li elements:

$("#items").on("click", "li", function () {     alert( $(this).html() ); }); 

now our event never needs re-bound, since #items loaded page, , never goes anywhere. click event bubbles element nested list item (whether loaded via ajax or not) captured , handled.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -