Samantha J T Star
Samantha J T Star

Reputation: 32758

Calling a function when document is ready?

When my document is ready I do the following:

$(document).ready(function () {
    $('#ReferenceID').change(function () { reLoad(); });
});

However I would also like to trigger an execution of the reLoad() function when the document becomes ready.

How can I call that function?

Inside reLoad() I would like to have something to put up a message showing the word "Loading". Something like jQgrid when it loads data. Is that easy to do?

Upvotes: 0

Views: 109

Answers (5)

Mark Schultheiss
Mark Schultheiss

Reputation: 34158

I know you have your answer but you could also do this:

$(document).ready(reLoad);

This uses your function (reLoad) as the document ready event handler.

Upvotes: 0

Maxim Krizhanovsky
Maxim Krizhanovsky

Reputation: 26699

First, you can bind reLoad directly to the Change event

$(document).ready(function () {
    $('#ReferenceID').change(reLoad);
});

And to call it, either trigger change, or call it directly

$(document).ready(function () {
   $('#ReferenceID').change(reLoad);
   $('#ReferenceID').trigger('change');
});

Upvotes: 2

amd
amd

Reputation: 21442

simply :

$(document).ready(function () {
 reLoad();
    $('#ReferenceID').change(function () { reLoad(); });
});

Upvotes: 2

devnull69
devnull69

Reputation: 16544

Like that

$(document).ready(function () {
    reLoad();
    $('#ReferenceID').change(function () { reLoad(); });
});

Upvotes: 1

James Allardice
James Allardice

Reputation: 165941

If I've understood you correctly, then all you need to do is call the function in the ready event handler:

$(document).ready(function () {
    reLoad(); //Calls reLoad
    $('#ReferenceID').change(function () { reLoad(); });
});

Note that if you don't need to do anything else in the change event handler, and you don't need to pass any arguments to reLoad, you could just pass a reference to reLoad to the change method:

$('#ReferenceID').change(reLoad);

Upvotes: 2

Related Questions