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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:23:50+00:00 2026-06-14T13:23:50+00:00

I was wondering if passing this to the jQuery function actually causes it to

  • 0

I was wondering if passing this to the jQuery function actually causes it to search in the DOM for it. The question has a specific context.

Let’s say I have:

$('#foo').click(function(){
  var id = $(this).attr('id');
  var someVal = $(this).data('someVal');
}

Will jQuery query the DOM to provide its functions or are all the information read and taken from the JavaScript object this?

And is there a performance difference to:

$('#foo').click(function(){
  var elem = $(this);
  var id = elem.attr('id');
  var someVal = elem.data('someVal');
}
  • 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-14T13:23:51+00:00Added an answer on June 14, 2026 at 1:23 pm

    It doesn’t query the DOM in this instance. $() wraps the this (or whatever else you have inside it) with a jQuery wrapper object.

    By caching it:

    var $this = $(this);
     // you will see code have a $ before or after a variable ( $this, this$, etc )
     // signifying it is a jQuery wrapped object
    

    You have the performance saving of only wrapping it with jQuery once. As opposed to having it go inside jQuery and wrap it over and over again. It is good coding practice to cache it.

    note: Of course if you have $('#whatever') it will be querying the DOM, since you have provided a selector for it to retrieve, then it wraps it with jQuery. So if you’ll be reusing it over and over it makes sense to save it as well! var $whatever = $('#whatever');

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

Sidebar

Related Questions

I'm just wondering why passing a System.Collections.Generic.List<string> into this function test(ICollection<object> t) will not
This question received great answers in jquery and I was wondering if someone could
Wondering what the best / good way of doing this would be in jQuery.
I'm somewhat new to jQuery and am just wondering how I go about passing
I'm currently wondering if there is a better solution than passing this scope to
Possible Duplicate: passing 2D array to function My question is related to passing an
I was wondering about the use of pthread_key_create while passing in a destructor function.
Wondering if anyone out there has ran into this before.... I'd like to use
I was wondering what is the best practice re. passing (another class) amongst two
For this question, I'm using ASP.NET Web Forms in C#, a web service and

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.