[jQuery] Question about $(this) and just "this"
I'm studying the excellent jQuery book at the moment. I bought it
yesterday and I have a question about chapter 3, page 43.
There's this stylesheet switching function:
$(document).ready(function() {
$('#switcher .button').bind('click', function() {
$('body').removeClass();
if(this.id == 'switcher-narrow') {
$('body').addClass('narrow');
}
else if (this.id == 'switcher-large') {
$('body').addClass('large');
}
$('#switcher .button').removeClass('selected');
$(this).addClass('selected');
});
});
I was wondering what's the difference between $(this) on the last line
and just "this" on the if/else lines?
I know that $(this) refers to the DOM element to which the behavior
was attached.
But what about this.id? Could it be written as $(this).id as well?