3

I am confused on the differences between these two code blocks:

$("#someButton").click(function() {
    var button = this;
    $(button).attr('disabled', 'disabled');
}

$("#someButton").click(function() {
    var button = $(this);
    $(button).attr('disabled', 'disabled');
}

Notice the difference of what the button variable stores. What's the point of storing $(this) into the button variable instead of just this? In the end, I am still using $(button).jQueryMethod() to manipulate it, not button.jQueryMethod().

1
  • 1
    Others have answered, but I would like to suggest that if you follow the second example (and I think you should), you might want to rename your variable to show that it is indeed a jquery object. I do this by naming my jquery object variables with a $ at the front. ie 'var $button = $(this); $button.prop('disabled');' Commented Nov 15, 2011 at 16:14

5 Answers 5

2

The difference isn't that significant in your example as you are only using the wrapped JQuery object once. This issue becomes more relevant if you need to use the JQuery object many times.

$("#someButton").click(function() {
    var button = this;
    $(button).attr('disabled', 'disabled');
    $(button).someJQueryMethod();
    ...
    $(button).someOtherJQueryMethod();
}

In this case it is better to wrap the object once and cache the results. It is a convention to cache the result in a variable starting with a $ sign to indicate that it contains a wrapped JQuery object.

$("#someButton").click(function() {
    var $button = $(this);
    $button.attr('disabled', 'disabled');
    $button.someJQueryMethod();
    ...
    $button.someOtherJQueryMethod();
}

This way the call to $() is only invoked once. This becomes particularly relevant if the reference is inside a loop.

Sign up to request clarification or add additional context in comments.

Comments

2
$("#someButton").click(function() {
    var button = $(this);
    $(button).attr('disabled', 'disabled');
}

is redundant

becuase you make jquery Twice !.

$("#someButton").click(function() {
    var button = $(this);
   button .attr('disabled', 'disabled');
}

3 Comments

I had no idea I could just call button.attr('disabled', 'disabled') For some reason, I thought it always had to be passed as a selector to the jQuery object like $(button).attr('disabled', 'disabled')
p.s. from now on use prop and not attr.
Haha, there's another thing I didn't know they added. Just saw that it was added in 1.6. Thanks! Reading over the documentation on .prop() it seems that .attr() is still used for getting attributes like href and title, is that correct?
2

In sample 1, button is a regular DOM object because on click callbacks, this just points to a regular DOM object, not a jQuery object. This is fine if you do not plan on doing any additional jQuery operations on this. However, you are disabling the button in the next line using a jQuery call to .attr(). I would use the second block written like this:

$("#someButton").click(function() {
    $(this).attr('disabled', 'disabled');
}

That way you can eliminate an extra variable that you don't seem to be using and also remove an extra query operation for $(button)

3 Comments

Thanks. Your code is definitely superior to what I had since I don't really need that variable, but I was just trying to figure out what the difference was between those two cases.
Did I explain what the difference was thoroughly enough?
Yeah, makes perfect sense. I understand when to use each case now.
1

In the second case, you can do this:

button.attr('disabled', 'disabled');

It is common to name jQuery variables with a $ prefix:

var $button = $(this);

$button.attr('disabled', 'disabled');

Comments

0

You would need to use $(this) if you want to use the jQuery API on the element. In your example you're just trying to get the element though, so both approaches would work.

simply getting the element though, so both techniques would work.

When to use $(this):

var button = $(this).css({ "border" : "1px solid red" });

When you can just use this:

var button = this.style.border = "1px solid red";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.