views:

26

answers:

1

I have this piece of code

$('ul.fonts li', '#wizard').live('click', function(e){
$('ul.fonts li', '#wizard').removeClass('selected');
$(this).addClass('selected');
$('blockquote').css('font-family', $(this).find('a').attr("class") )
e.preventDefault();
})

I tried ti optimize this code storing the selector in a variable.

var $fonts_links = $('ul.fonts li', '#wizard')
$fonts_links.live('click', function(e){
$fonts_links.removeClass('selected');
$(this).addClass('selected');
$('blockquote').css('font-family', $(this).find('a').attr("class") )
e.preventDefault();
})

It works, except from the third line. `$fonts_links.removeClass('selected'); This is so weird..some advice?

+2  A: 
T.J. Crowder
However you could cache `$(this)` within the `click` handler, since there's no need to build the same jQuery object twice => `var $this = $(this);`
Peter Ajtai
Thanks a lot, I just started to use caching in Jquery
framomo86
@Peter: Can and should, I didn't notice he was using it twice. Fixed, thanks.
T.J. Crowder