Link to home
Create AccountLog in
Avatar of Pedro Chagas
Pedro ChagasFlag for Portugal

asked on

JavaScript / Jquery - variables inside functions (global)

Hi E's, I need to know how I can use a variable created inside function and use them out side function.
I have this code:
    $("#b_ocultar").click(function(){
        $("#logo").fadeOut(500);
        $("#b_ocultar").fadeOut(500);
        $("#cabecalho").delay(500).hide(500);
        var ocultar = 1;
    });

Open in new window

What I have to do for I can use variable "ocultar" outside the function?
I try to found in Internet for Global variables but I thing JavaScript don't have.

The best regards, JC
SOLUTION
Avatar of gavsmith
gavsmith
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
ASKER CERTIFIED SOLUTION
Avatar of Kiran Sonawane
Kiran Sonawane
Flag of India image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Just a quick note FWIW...

When you declare a global javascript variable as suggested in the accepted solution it is added to the window object anyway:

 var ocultar = 1234;

    $("#b_ocultar").click(function(){
        $("#logo").fadeOut(500);
        $("#b_ocultar").fadeOut(500);
        $("#cabecalho").delay(500).hide(500);
       alert(window.ocultar); //Will display 1234
    });

Open in new window


Regards