Tutorial :Can I call the function Ready() again in jQuery



Question:

I have this code

   $(".insert").click(function(){              $(".insert").ajaxStop(function(){                  $(".load").hide();              });              $(".insert").ajaxStart(function(){                  $(".load").show();              });                $.ajax({                  type: "GET",                  url: "edit.php",                  data: "action=add",                  success: function(msg){                        $(".control").append(msg);                  }              });              });  

as you can see this code append the HTML response of edit.php to the .control

the problem is

after appending the html .. all jquery changes wont apply in it .. because the $(document).ready() was already called before this this HTML code was born ...

can I call $(document).ready() every while I do any changes ????


Solution:1

Take a look at jQuery live. It is meant to bind events automatically for new elements. It works for click, dblclick, mousedown, mouseup, mousemove, mouseover, mouseout, keydown, keypress, and keyup.


Solution:2

If you could elaborate on what you are doing in your document.ready function, I could perhaps give more specific help. You might find what you need in the live() function, which simulates applying events to objects even if they were added to the DOM after calling live().

To answer your question though, yes you can invoke the event handler just by doing this:

$(document).ready();  


Solution:3

I solved this demand with off and on again event for eg :

        $("div.value").off("click");          $("img.cancelEdit").off("click");            $("div.value").on("click", function (e) {              var $this = $(this);              $this.hide();              $this.next().show();          });            $("img.cancelEdit").on("click", function (e) {              var $this = $(this);              $this.parent().hide();              $this.parent().prev().show();          });  

and You can use Selector.Live() ;


Solution:4

Yes, calling the ready function with an argument (whether it's a reference to a function or an anonymous function), will append it to the chain of functions jQuery calls on the event.


Solution:5

Depending on the structure of the DOM manipulation you are doing, you might be able to use event delegation to apply event handlers to newly created DOM elements.

http://www.danwebb.net/2008/2/8/event-delegation-made-easy-in-jquery


Solution:6

If you need that to run when the document is ready, wrap it in $(document).ready(function(){}); and it will run at the appropriate time.

You can add rules to the .ready() method of document in multiple places.


Solution:7

Use on() instead of live(). live() has some drawbacks and it is depreciated.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »