Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - function inside $(document).ready function

Is it correct to create functions inside of

$(document).ready(function() { 

like so:

$(document).ready(function() {      function callMe() {       }  }); 

The function inside of the .ready() does not have to call before DOM is ready and event inside of the ready() is triggered.

Just to clarify a little bit - here's the code which would illustrate the problem:

$(function() {     var ind = 0;      // some event is executed and changes the value of the ind      // another event which affects the ind variable      // and another one - after this event we call our function       // there's another event - and we call our function again 

The function which I need to call needs the updated value of the ind variable - which I guess I could pass as a parameter, but is there a better way of doing it?

Also - another important thing is that the function() in question can also change the value of the ind variable - for instance incrementing it (ind++).

like image 580
user398341 Avatar asked Jul 21 '11 18:07

user398341


People also ask

Can I write function inside document ready?

Yes, you can do that, it's just a matter of scope. If you only need to access callMe() from within $(document). ready(function() { }) , then it's fine to put the function there, and offers some architecture benefits because you can't access the function outside of that context.

What is the role of given function in jQuery $( document .ready function ()?

$( document ). ready()A page can't be manipulated safely until the document is "ready." jQuery detects this state of readiness for you. Code included inside $( document ). ready() will only run once the page Document Object Model (DOM) is ready for JavaScript code to execute.

Can we write function inside function in jQuery?

Yes, you can.

How do you call a function in document ready?

jQuery Document Ready Example ready(function() { //DOM manipulation code }); You call jQuery's $ function, passing to it the document object. The $ function returns an enhanced version of the document object. This enhanced object has a ready() function you can call, to which you pass a JavaScript function.


1 Answers

Yes, you can do that, it's just a matter of scope.

If you only need to access callMe() from within $(document).ready(function() { }), then it's fine to put the function there, and offers some architecture benefits because you can't access the function outside of that context.

If you need to use the callMe() function outside of document ready though, you need to define the callMe() function outside of that context.

function callMe() {   // Do Something }  $(document).ready(function() {   callMe(); }); 

UPDATE

Based on your clarification, you have two options:

1) DECLARE variable outside of ready(), but then define variable inside of ready():

var someVariable; function callMe() {   someVariable++;   alert(someVariable); }  $(document).ready(function() {   someVariable = 3;   callMe(); // Should display '4' }); 

2) Within ready(), define variables using window.yourVariable = 'whatever';

like image 191
Mike Richards Avatar answered Sep 30 '22 15:09

Mike Richards