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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:18:43+00:00 2026-06-09T03:18:43+00:00

I have a $.fn.sample = function() called by, let’s say $(‘#example’).sample(); so inside the

  • 0

I have a $.fn.sample = function() called by, let’s say $('#example').sample(); so inside the function I could use “this” into it’s scope:

$.fn.sample = function(){
   console.log($(this).width()); 
} //In this case, it would log the width of #example 

But let’s say I call a hover function into another element, like this

$.fn.sample = function(){ 
  console.log($(this).width()); 
  $('#other').hover(function(){
    //Here, $(this) will refer to #other
  });
}

So, inside the hover function “$(this)” will refer to #other, is there a way to use the “parent” $(this)? In this case, the “#example” inside this hover function?

  • 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-09T03:18:46+00:00Added an answer on June 9, 2026 at 3:18 am

    The answer is yes, but not with parent.

    A common solution for your question is to use a ‘that’ variable:

    $.fn.sample = function(){ 
      console.log($(this).width());
    
      var that = $(this);             // <-- good pattern for traversing scope.
           
      $('#other').hover(function(){
        
        //Here, that will refer to the parent.
      });
    

    }

    I believe this was originally proposed by Douglas Crockford, but I am not certain of the origin. The link will give the technical details, but the usage turns out to be very important for ‘private data members’.

    Another very important point on Best-Practice…

    I really recommend using the pattern but not calling the variable ‘that’.

    Here is why:

    It is not so important to know -> where a variable came from, but -> what it is.
    In practice, a that can be coming from a wrapping scope many lines of code away from the current line in question. From a maintainability standpoint, it is a waste of time to try and figure out what ‘that’ is, and even more frustrating if it is not even known what ‘this’ is. Instead, we should just call it what it is and let the scope be what it is.

    For example,

    var button_container; //instead of that.
    

    Also, others are using the naming convention of adding a dollar sign.

    var $name;
    

    This is ok, but can be confusing. It is worth mentioning that it indicates the object is a jQuery object.

    Hope that helps.

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

Sidebar

Related Questions

Let's say I have just called a function, f , and an error occurred
Let's say I have a simple class called WebsterDictionary that has a function that
Let's say I have the following code: typedef std::function<void ()> func_type; void some_func() {
I wrote this very simple function for my current project called insidelabel() that let's
I have a function (say foo() )that will be called from time to time
Let's say I have a controller template with a before function like so... public
I have a small sample function: #define VALUE 0 int test(unsigned char x) {
I have a sample class where I need to specialize the Print function if
I have a script.I will show sample. <form> <textarea id='input'></textarea> <input onclick=convert() value=go function
I have a simple function that returns an NSString after decoding it. I use

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.