Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can jQuery ready() function be used twice for the same element?

I want to use jQuery ready() function for the document element. Here are my scripts:

1st page:

$(document).ready(function(){
            $('form#haberekle').ajaxForm(options);
});

2nd page:

    $(document).ready(function() { 
    var options = { 
        success:showResponse,
        beforeSubmit:showRequest,
        resetForm:true
    };
    $('#haberresmiekleform').ajaxForm(options); 
});

These two pages are included in the same main page with <!--#include file=""-->

Can these two functions work properly, or they block each other? According to my experience they seem work properly.

For example: an onclick function of a button is only one.

like image 840
Yunus Eren Güzel Avatar asked Jul 02 '11 11:07

Yunus Eren Güzel


3 Answers

You can have as many .ready() calls as you want, jQuery is designed with this in mind and it's absolutely ok.

So yes, it is ok, and you won't have any problems...this happens all the time.

Think of it as an event handler, like .click(), this is exactly how it behaves (well, strictly speaking, not exactly, but for most purposes like this). So you can have as many as you want.

One more note that may be of interest, the handlers you pass into .ready() are pushed via .done() to the readyList, which means they'll execute in the order you called them in the page. The same order behavior is true (though via an array, a different method) in earlier versions of jQuery.

like image 115
Nick Craver Avatar answered Nov 08 '22 16:11

Nick Craver


Yes, that is fine. Both will be called when the dom is fully loaded. Note that if you call .ready() after the dom is already loaded, the callback will be executed immediately. See http://api.jquery.com/ready/

like image 2
Rob Cowie Avatar answered Nov 08 '22 17:11

Rob Cowie


Yes, you can.

Take a look here: http://www.learningjquery.com/2006/09/multiple-document-ready

like image 2
SunnyRed Avatar answered Nov 08 '22 17:11

SunnyRed