Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6643015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:59:55+00:00 2026-05-25T23:59:55+00:00

Can you set variables in the beginning of a file to be set through

  • 0

Can you set variables in the beginning of a file to be set through out?

Example:

var works = $('#id1'); 
var span = $('.icon');

works.focus(function() {
    if(this.value.length == 0)
    span.hide();
});

works.keyup(function() {

if(this.value.length > 0 && !span.is(':visible'))
        span.stop().show();
    else if(this.value.length == 0)
        span.stop().hide();
});

The variable span inside the function doesn’t work. Instead of adding var span = $('.icon'); in each function is it possible I can make it work?

Thanks alot

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:59:56+00:00Added an answer on May 25, 2026 at 11:59 pm

    Yes, you can have global variables, but if you initialize them in a <script> block in the <head> they’ll be empty. The DOM won’t have been loaded.

    You can instead do this:

    $(function() {
      var span = $('.whatever');
    
      $('something.else').click(function() {
        // span will work here
      });
    
    });
    

    Not quite the same as globals, but in a way that’s better since true globals are kind-of a bad idea. Anything declared with var inside the $(function() { ... }) code will be effectively global to all code in that initialization function.

    Oh, and note that putting the initialization code in a function invoked with $() is the jQuery way of deferring execution until the DOM is ready. That will work in a script in the <head>, and it’ll also work if you import your scripts at the end of the <body> as many luminaries now advise.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've noticed that you can set javascript variables using ASP code. For example: var
Can I set variables inside if-construct? In general, where is it allowed to set
I assume that MAMP environment variables can be set in ~/.bash_profile, but it seems
How can I use variables to set my timer? I want to control delay
I have a separate file where I include variables with thier set value. How
In JavaScript, you can set a variable equal to a method like this: variable
How can I can set a global variable for the username of the logged-in
With Ruby, how can I set a variable based on a condition? Something like:
How can I set a global variable in a C# web application? What I
I am trying to set the CLASSPATH variable so that my java programs can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.