Understanding jQuery's 'this' Keyword

  • Thread starter jeff1evesque
  • Start date
In summary, the keyword 'this' can refer to either a DOM object or a jQuery object within jQuery. It is used in different ways depending on the context, such as being inside a callback function or within own jQuery functions. The segment of code provided shows examples of both uses. Question #3 is asking why the keyword 'this' is necessary in the statement "this.Counter = 1" while the others could potentially be removed or replaced with the element's name. The answer provided is that 'this' is needed because Counter was not previously defined and is being declared within the jQuery object Slideshow. Question #2 and #1 are related and suggest that the keyword 'this' may be more of a stylistic convention rather than necessary
  • #1
jeff1evesque
312
0
I've read in several sources that the keyword 'this', and have come up with the following summary:

There are two "main contexts" where the keyword 'this' is used within
jQuery. The first refers to a DOM object, and the second to a jQuery
object. 'this' is a DOM element when we are inside of a callback function
(in the context of jQuery for example, being called by the click, each,
bind, etc. methods). 'this' is a jQuery object when we are inside own
jQuery functions.

But I am having difficulty with the context it is used, and having a difficult time trying to understand it's equivalent. I've also attached the HTML file (if necessary, and if people have the time to read)

-------------------------------------------------------------------------------------
This is a .js segment of code I would like to look at (for those that also have the time):
-------------------------------------------------------------------------------------

//jQuery allows for the web-page to behave dynamically.

var holder = $.fn;
holder.extend({
SplitID : function()
{
return this.attr('id').split('-').pop();​
},​

Slideshow : {
Ready : function()
{
$('div.tmpSlideshowControl')
.hover(
function() {
$(this).addClass('tmpSlideshowControlOn');​
},​
function() {
$(this).removeClass('tmpSlideshowControlOn');​
}​
)​
.click(
function() {
holder.Slideshow.Interrupted = true;

$('div.tmpSlide').hide();
$('div.tmpSlideshowControl').removeClass('tmpSlideshowControlActive');

$('div#tmpSlide-' + $(this).SplitID()).show()
$(this).addClass('tmpSlideshowControlActive');​
}​
);​

this.Counter = 1;
this.Interrupted = false;

--------------------------------------------------------------------------------------
My Question:

1.)
Every keyword 'this' could be removed from the segment of code above, except for 'this.Counter = 1'. But for the rest of the code why not just remove it? For instance "$(this).whatever." could simply just be written as "$.whatever".

2.)
Ready() is not a callback function, so the keyword 'this' used in this segment refers to a jQuery element. Instead of writing 'this', could we write the element's name in its place? For instance $(element's name).removeClass('tmpSlideshowControlOn'); (elements name for a slide show would be the ID name defined in the HTML?)?​

3.)
Why is the keyword 'this' is required in this.Counter = 1? Is it because 'this' refers to a DOM element, or because Counter is a new variable, I am pretty confused? When I wrote this.Counter=1 as holder.Slideshow.Counter = 1, the page loaded the same- from which I concluded both are the same. But the question remains, why is this required here, and other portions (above that particular line) of the code, I could remove the keyword 'this'?​

Any help would be great, thanks.​
 

Attachments

  • segment of HTML file.txt
    2.5 KB · Views: 440
Last edited:
Technology news on Phys.org
  • #2
Question #3:
I think the answer to #3 is because Counter was not previously defined, so we had to define it within the jquery object Slideshow. Otherwise the variable Counter would be syntaxically incorrect.

Question #2 (and thus #1):
All the references to the keyword 'this' could be replaced with holder.Slideshow (or could be completely removed, unless a new variable is being declared), which leads me to conclude that the keyword 'this' is something of a convention of style (in my opinion useless). Thus, I feel that I've answered my own questions, but if I'm wrong in anyway, could someone be kind enough to let me know.

Thanks,

JL
 
Last edited:
  • #3
'this' is a keyword in Javascript (and Java) and is not specific to JQuery. This site should help with what you're trying to understand: http://www.quirksmode.org/js/this.html"
 
Last edited by a moderator:

1. What is the purpose of jQuery's 'this' keyword?

The 'this' keyword in jQuery refers to the current HTML element that is being manipulated. It allows for more efficient and concise coding by avoiding the need to repeatedly select the same element.

2. How is the 'this' keyword different from the 'event.target' property?

While both the 'this' keyword and the 'event.target' property refer to the current element being manipulated, the 'this' keyword is a more general term that can be used in a wider variety of situations, while the 'event.target' property specifically refers to the element that triggered the event.

3. Can the 'this' keyword be used within an anonymous function in jQuery?

Yes, the 'this' keyword can be used within an anonymous function in jQuery. However, it may behave differently depending on the context in which it is used. It is important to always test and debug code when using the 'this' keyword within anonymous functions.

4. How does the 'this' keyword behave in a jQuery event handler?

In a jQuery event handler, the 'this' keyword refers to the element that triggered the event. This allows for easy manipulation of the specific element without needing to select it again.

5. Can the 'this' keyword be used in jQuery methods that do not involve events?

Yes, the 'this' keyword can be used in jQuery methods that do not involve events. For example, it can be used in methods such as .each() and .click() to refer to the current element being iterated over or clicked on, respectively.

Similar threads

  • Programming and Computer Science
Replies
2
Views
4K
  • Programming and Computer Science
Replies
3
Views
316
  • Programming and Computer Science
Replies
2
Views
4K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
5
Views
9K
  • Programming and Computer Science
Replies
13
Views
1K
  • Programming and Computer Science
Replies
12
Views
3K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
6
Views
4K
Back
Top