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

The Archive Base Latest Questions

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

Ive been starting to do javascript and jQuery recently and one thing I constantly

  • 0

Ive been starting to do javascript and jQuery recently and one thing I constantly find myself wondering is when to use “$” I know that indicates jQuery but it just doesn’t always seem to be that way. I’ll give some examples:

These are two scripts I’ve written:

First:

$(function() {
    var newHTML = '<span style="font-size: 1.7em; text-align:center; line-height:50px;">Login</span>';
    var oldHTML = '<span style="font-size: 32px; line-height: 18px;">+</span><span style="font-size: 14px; float: left;">Add to watchlist</span>';

    // on mouse over
    $("a.bid-addwatchlist").hover(
    function () {
        (this).innerHTML = newHTML;
    },
    // on mouse out
    function () {
        (this).innerHTML = oldHTML;
    });
});

Second:

(function(){
    $("#container a").click(function(){
        if ($(this).html() == "Stop Listening")
        {
            $(this).html("Listen");
        }
        else if ($(this).html() == "Listen")
        {
            $(this).html("Stop Listening");
        }
    });
});

Why is it that in the first script it wouldn’t work if I had a $ before “this” but the second script needed it?

Note: I did already look here: When to use $ and when not to

But that answer was not nearly comprehensive enough.

  • 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:55:56+00:00Added an answer on May 25, 2026 at 11:55 pm
    $(function(){
      // ...
    });
    

    is equivalent to:

    $(document).ready(function () {
      // ...
    });
    

    This is because jQuery itself will execute the function on the ready event.

    On the other hand the below creates an anonymous function that isn’t executed:

    (function(){
      // ...
    });
    

    What you’d need instead is:

    (function(){
      // ...
    })();
    

    and that would execute the function immediately.

    The first script doesn’t actually need the $(this) unless you want to use jQuery functions as the second function one does.
    Although it is good to note that the second function isn’t executed as stated above unless you have other code that isn’t shown here.

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

Sidebar

Related Questions

I mainly code small programs for myself, but recently, I've been starting to code
I'm starting to re-skill myself in Desktop applications. I've been doing Web applications for
I've been banging my head over this. Using jquery or javascript, how can I
I'm working on a javascript application that makes intensive use of math operations, dozens
I'm starting a new project that will be heavy on javascript and am looking
I'm very new to JavaScript let alone JQuery, Although I've been picking it up
I'm a Delphi, Ruby, and Javascript programmer that is finally learning C - starting
I'm starting out with Lift and been reading a few tutorials and books that
I use this script daily and never had an issue before now. Ive been
I've been thinking about starting a new project in C#.NET (4.0RC) for a couple

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.