Avatar of Pedro Chagas
Pedro Chagas
Flag 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
JavaScriptjQuery

Avatar of undefined
Last Comment
gavsmith

8/22/2022 - Mon
SOLUTION
gavsmith

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Kiran Sonawane

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Kyle Hamilton

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
gavsmith

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
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck