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

  • Home
  • SEARCH
  • 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 7006721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:30:49+00:00 2026-05-27T21:30:49+00:00

I often see examples of using the keyword this in jquery. Sometimes I see

  • 0

I often see examples of using the keyword this in jquery. Sometimes I see it used with the $ and parenthesis, other times without. And I thought I saw it used with a little of each.

So,

 var id = this.attr('id');

 var id = $(this).attr('id');

 var id = $this.attr('id');

Are these all the same? Is there a preferred way? Is this a javascript thing and $(this) a jQuery thing? If so, where does $this fall?

I know this is probably a total newbie question, but I haven’t been able to get the simple, this, by itself, to work. I can only get $(this) to work. I’m not sure if I’m doing something wrong, or if I’ve been reading examples with typos.

  • 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-27T21:30:51+00:00Added an answer on May 27, 2026 at 9:30 pm

    this is a JavaScript thing. It refers to the “context” a function is running in. For most event handlers, it is the (“raw”) DOM element that is listening to an event. In other situations it will mean other things; Googling “this in JavaScript” might be enlightening.


    I say it is the “raw” DOM element because jQuery is often used to wrap plain DOM elements in a jQuery wrapper, so you can use jQuery methods like attr instead of the usual ones (getAttribute, setAttribute, etc.). This wrapping is accomplished with the $ function, and that’s where you see $(this). For example:

    this.getAttribute("href")
    /* or */ someElement.getAttribute("href")
    

    is the same as

    $(this).attr("href")
    /* or */ $(someElement).attr("href")
    

    $this or this$ is just a variable name. But, it is often conventional to do an assignment like

    var $this = $(this);
    

    The reason for this is to avoid continually invoking the $ function, which is somewhat expensive as it creates a new jQuery wrapper object every time. If you store the wrapped element in a variable, you gain slightly in efficiency.


    In rare cases, this might already be a jQuery wrapper. The case that comes up often for me is when writing jQuery plugins. In that case you can do things like this.attr("id") directly, without wrapping it up first, because it’s already wrapped. In the usual cases (event handlers, $.each, etc.) the wrapper is necessary.

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

Sidebar

Related Questions

I quite often see on the Internet various complaints that other peoples examples of
You often see, on sites like The Daily WTF , examples of overengineered code
I often see instances in which using a macro is better than using a
If you're doing MVVM and using commands, you'll often see ICommand properties on the
Quite often I see source code where language's keyword are replaced with full type
I have been looking into custom allocators and I quite often see them using
I think this mostly applies to web applications, since you often see things like
I have often used this code to determine if an array is really an
I often find myself using inline initialization (see example below), especially in a switch
I see often (rewritten) URLs without ID in it, like on some wordpress installations.

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.