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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:14:14+00:00 2026-05-16T05:14:14+00:00

jQuery is currently providing me with a fun introduction to Javascript after 12 years

  • 0

jQuery is currently providing me with a fun introduction to Javascript after 12 years of surviving happily without. I’m at the stage where I’m trying to learn as much as I can about optimising the code I write and, whilst I have found plenty of good reference material, there is something quite basic which is puzzling me and I have been unable to find anything about it anywhere.

When I’m attaching something to an element how should I be referring to that element within the function. For example, when attaching a function to an element’s click event :

$('#a_button',$('#a_list_of_buttons')).click(function() {
    // NOW WHAT'S THE BEST WAY TO REFER TO '#a_button' ?
});

I know not to keep re-selecting it like so as the browser has to search the whole DOM again from scratch to find what it’s already found once :

$('#a_button').click(function() {
    // I KNOW THAT THIS IS NAUGHTY
    var buttonValue = $('#a_button').val();
    $('#a_button').addClass('button_has_been_clicked');

});

Currently I’m using either of the following but am not entirely sure what each is actually doing :

$('#a_button').click(function() {
    // USING this
    var buttonValue = $(this).val();
    $(this).addClass('button_has_been_clicked');
});

But is this just re-selecting like in the first “naughty” example?

$('#a_button').click(function(event) {
    // USING event.target
    var buttonValue = $(event.target).val();
    $(event.target).addClass('button_has_been_clicked');
});

This seems like it might be better but is it efficient to refer to ‘event.target’ multiple times?

$('#a_button').click(function(event) {
    // USING A LOCAL VARIABLE
    var thisButton = $(this);

    // OR SHOULD THAT BE
    var thisButton = $(event.target);

    var buttonValue = thisButton.val();
    thisButton.addClass('button_has_been_clicked');
});

I understand the performance efficiencies of passing things to variables but I’m unsure whether or not in these situations using $(this) or $(event.target) provides me with the same efficiencies already and so by setting a new variable I’m actually doing more work that I need to.

Thank you.

  • 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-16T05:14:15+00:00Added an answer on May 16, 2026 at 5:14 am

    I may be wrong, but this and event.target are both just different references to the same element.

    this and event.target are not always references to the same element. But in answer to your question, var thisButton = $(this); is definitely the winner. If you were writing C# code, you would never do the following:

    this.Controls[0].Controls[0].Text = "Foo";
    this.Controls[0].Controls[0].Controls.Clear();
    

    You would do this:

    var control = this.Controls[0].Controls[0];
    

    So you probably should never re-use $(this) more than once either. Althought it’s trivial to convert this from a DOM element to a jQuery object, it’s still an unnecessary overhead.

    However, sometimes you need to gear back from optimisation to make sure your code maintains it’s readability.

    Another option of course is just to change what this is. This is javascript afteral:

    this = $(this); // Now `this` is your jQuery object
    

    Disclaimer: I only just tried the above and it seemed to work. Might have some issues though.

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

Sidebar

Related Questions

am currently studying jquery, can someone tell me the things that javascript native can
A] Technologies being used: 1] Javascript 2] Jquery (elements currently being used are Jquery
im currently learning jQuery and have ran into a problem. I am trying to
I'm currently using jQuery to make a div clickable and in this div I
I'm currently using JQuery to assign colors to containers. In Chrome and IE, when
I'm currently using jQuery Cookies plugin, and depending on which side of the page
Currently my jQuery slider prevents the user from progressing if the range is 0-0
I am currently using jQuery to load a variable php layout depending on the
I am currently using jquery corner for making a div round cornered. But the
I have a problem with my jquery UI tabs currently only in Firefox 3.6.24

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.