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 7896039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:47:27+00:00 2026-06-03T07:47:27+00:00

I am trying to better understand how variables are stored with JavaScript and jQuery.

  • 0

I am trying to better understand how variables are stored with JavaScript and jQuery.

Using the below code, a separate variable called default_value is created for each .default-value element. Is this correct? Can it only be accessed from within the anonymous function that created it? I’ve heard the term “namespace”, and does it apply? Please provide any additional details so I can better understand what is going on.

Next, if I wanted to apply this to an element with a given ID instead of a group of a given class, then using each() appears unnecessary. How should it be modified?

$('.default-value').each(function() {
    var default_value = this.value;
    $(this).focus(function() {
        if(this.value == default_value) {
            this.value = '';
        }
    });
    $(this).blur(function() {
        if($.trim(this.value) == '') {
            this.value = default_value;
        }
    });
});
  • 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-06-03T07:47:28+00:00Added an answer on June 3, 2026 at 7:47 am

    What you are talking about is scoping.

    Basically when you are in a function and use the var keyword (which you should) the variable is only available in the current scope (in your case in the anonymous function).

    If you would have skipped the var keyword the variable would be available in the global scope.

    When you want to get an element with an id (which is ALWAYS unique) in the DOM you could simply do:

    $('#theid').focus(function() { });
    

    Another thing you could do is using Jquery’s hover which handles both the .focus() and the .blur():

    $('#theid').hover(function() {
      // focus in your code
    }, function() {
      // blur in your code
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to understand Ruby a bit better, I ran into this code surfing the
I've been trying to read and understand a bit more about better code and
I'm trying to understand better how to declare a variable (string) and how methods
I'm trying to better understand prototypes in JavaScript, so it made sense to me
I am trying to understand the for loop better. I have the following variables:
I am trying to better understand basic concepts in OOP. What are static and
I'm trying to better understand the concept of 'autocommit' when working with a Postgres
I am trying to better understand when I should and should not use Iterators.
I'm trying to better understand what the stack trace means. I have a two
I've seen some examples, but trying to better understand how Resource files work, such

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.