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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:22:25+00:00 2026-05-23T02:22:25+00:00

I have this simple ghost text implementation: HTML code: <div id=searchPanel> <form method=get id=searchBox

  • 0

I have this simple ghost text implementation:

HTML code:

<div id="searchPanel">
     <form method="get" id="searchBox" action="somePage.php">
     <input class="ghText" type="text" name="query" value="search here"/>
     </form>
</div>

jQuery code:

$(document).ready(function(){
        $txtField = "#searchPanel form input.ghText";
        var value = $($txtField).val();
        $($txtField).focus(function(){
            if($(this).val() == value)
                $(this).val("").removeClass("ghText");
        });
        $($txtField).blur(function(){
            if($(this).val()==""){
                $(this).val(value).addClass("ghText");
            }
        });
});

The example above is not going to work. When the user focuses the cursor on the search bar, the class “ghText” wont be removed for some reason.

However now if I change the “var value” (variable initialization) and “value” with
“$value” as in:

$value = $($txtField).val(); 
$(this).val($value).removeClass("ghText");
$(this).val($value).addClass("ghText");

everything works perfectly.

I can just go to sleep and not worried too much about it..but I am very curious why something like that can happen?

is it because of the “this” not referreing to the right object, or is it because i tried storing jQuery object in non-jQuery variable or is it about something else..can somebody point out to me what was wrong? I have always thought that “var x” is the same as “$x”..?

  • 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-23T02:22:25+00:00Added an answer on May 23, 2026 at 2:22 am

    You seem to be confused about JavaScript variables. There is no such thing as “jQuery variables” and “non-jQuery variables”. Some specific cases:

    • A variable declared with var is different to a variable without. “var x” is a local variable, so it will not share a value with other functions which also have a variable called “x”. This is almost always a good thing, so you should almost always declare variables with “var”.
    • The $ in jQuery is sort of special. It isn’t that special; it’s just that jQuery has declared a variable called “$” which does some fancy operations.
    • There is nothing special about variables that begin with “$”. In other words, “$x” is just a variable name. It is a different variable to “x”, and it isn’t a “jQuery variable”. It’s just a JavaScript variable called “$x”. (This is different from PHP, where the $ is actually a special variable syntax.)

    So you can just call it “value” instead of “$value”.

    Possibly the fact that you removed the “var” changed things by making it into a global variable.

    As for “this”, yes, that is a tricky aspect of JavaScript, and might be causing your problem. The value of “this” inside the inner ‘focus’ and ‘blur’ functions is likely to be different from the value of “this” outside. I’m not sure exactly what “this” refers to in an event handler, but it will not be the same object. So what you probably want to do is assign “this” to a variable in the outer function, and then refer to that variable on the inside in place of “this”.

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

Sidebar

Related Questions

i have this simple html form: <form action=test/ method=get accept-charset=utf-8> <input type=text name=first value=
I have this simple code: <html> <head> <script src=jquery-1.5.1.min.js type=text/javascript></script> <script type=text/javascript> $(function() {
I have this simple html piece of code, whenever I click on any of
I have this simple Lucene search code (Modified from http://www.lucenetutorial.com/lucene-in-5-minutes.html ) class Program {
I have this simple code that records appends a log to a text file:
I have this simple code that needs to get a chunk of a large
I have this simple: <script type=text/javascript> $(#message).fadeIn(slow); </script> <div id='message' style=display: none;> <span>Hey, du
I have this simple piece of code in c++: int main(void) { string text
I have this simple example I can't seems to get working : MERGE INTO
I have this simple code that speaks for itself. <script language='javascript"> function check() {}

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.