Friday, June 26, 2009

[jQuery] Re: call a method outside a jquery object

Thanks Eric,

Actually I really need to access to a method inside another function.
I created a simple example however my aim is calling a "public method" (let me use public) that has been defined inside a function like that

$(         function(){                 function openMenu(){                         doSomething(x);                 }                 function closeMenu(){                         doSomething(x);                 }          } ); 
the real method open a slide menu, in fact all the function define several method to manage this menu and I would like that openMenu is called when the page has been loaded.

For that reason, because I have already a function that contain a $(document).ready(function()
I simply wanted to nest the call to my "public method" inside that function.

Are you suggesting to add a $(document).ready(function() inside my slideMenu Function?

like that

$(         function(){ 		$(document).ready(function(){openMenu(); 		});                 function openMenu(){                         doSomething(x);                 }                 function closeMenu(){                         doSomething(x);                 }          } );

Please, I am very interested to the long short story, where can I read it?
Cheers



Eric Garside ha scritto:
Long story short, you can't do what you're trying to do. You have some massive scoping issues. first, anything within you document.ready function (as defined by $(funciton(){..});) that gets declared in there is accessible only inside that function itself.  Now, I'm not sure what you're actually trying to accomplish here, as this example is a bit on the simple side. Could you give a broader idea of your goal? That'd help in putting you on the right direction to the best way to get this done (as if you're merely trying to echo a variable, there's way better implementations than what you've described).  On Jun 26, 5:06 am, jasper saronno <jakirihutim...@gmail.com> wrote:   
Hello Everybody,  I am not sure I have explained well enought what I mean.  I found very difficult to call a method outside his scope.  For example I have 2 files js  myFunction.js  init.js (where I initialize all my page)  [init.js ] // JavaScript Document $(document).ready(function(){         //call getX();  });  -----------------------------------------------------  [myFunction.js ] // JavaScript Document   $(         function(){                 var x = "Hello world";                 function getX(){                         alert(x);                 }         } );  I tried many syntax in order to call the method outside its scope  $.getX();  $(function(){getX()});  $().function().getX();  $("myHtmlObj").click(function(){getX()});  I really would like to understand that issue, if you also may suggest the title of a good book and the chapter that explain it I would really appreciate.  Many thanks - jasper     
   

No comments: