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

  • Home
  • SEARCH
  • 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 3242122
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:16:41+00:00 2026-05-17T18:16:41+00:00

I have this piece of code $(‘ul.fonts li’, ‘#wizard’).live(‘click’, function(e){ $(‘ul.fonts li’, ‘#wizard’).removeClass(‘selected’); $(this).addClass(‘selected’);

  • 0

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?

  • 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-17T18:16:42+00:00Added an answer on May 17, 2026 at 6:16 pm

    You’ve said “It works, except [for] the third line.” but you haven’t said how it doesn’t work.

    But since the whole point of live is to handle your changing the DOM dynamically, you probably don’t want to cache the result of the selector in this situation: If you add more list items after you’ve cached the result, they won’t be added to the jQuery instance you’ve cached, and so you’ll end up missing those ones out (not removing the “selected” class from them) when the handler next runs. So if that’s what you’re seeing…

    If the wizard element is stable (not dynamic), you could do this:

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

    That way, you’re only caching the stable bit, and you’re explicitly telling jQuery what the root of the delegation is by using delegate (which is a variant of live that’s rooted in a specific element).

    Off-topic: Unless you’re seeing a performance problem with the original code in the wild, caching may not be your friend anyway — caching is a trade-off between memory use (and complexity) vs. speed-of-execution, probably best to only use it when faced with a tangible problem. But I’m assuming you’re doing this for a reason, hence the suggestion above.

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

Sidebar

Related Questions

I have this piece of code: $(#faq).click(function () { var url = $.get(faq, {
I have this piece of code: var myObj = function () { this.complex =
I have this piece of code (summarized)... AnsiString working(AnsiString format,...) { va_list argptr; AnsiString
I have this piece of code #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h>
I have this piece of code I'm trying to get to display but no
I have a piece of code looking like this : TAxis *axis = 0;
I have this piece of code on a php page, at the bottom: window.opener.location.href=/index.html;
I have this piece of code for my footer. the footer needs to have
I have this piece of code I've done so far: $assign = $_POST['assign']; if(!empty($name)
I have this piece of code in my Silverlight project: private void button1_Click(object sender,

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.