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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:29:50+00:00 2026-06-09T06:29:50+00:00

I have a set of elements like this: <ol id=selectable> <li class=ui-state-default>A</li> <li class=ui-state-default>B</li>

  • 0

I have a set of elements like this:

<ol id="selectable">
    <li class="ui-state-default">A</li>
    <li class="ui-state-default">B</li>
    <li class="ui-state-default">C</li>
    <li class="ui-state-default">D</li>
    <li class="ui-state-default">E</li>
    <li class="ui-state-default">F</li>
    <li class="ui-state-default">G</li>
    <li class="ui-state-default">H</li>
    <li class="ui-state-default">I</li>
    <li class="ui-state-default">J</li>
    <li class="ui-state-default">K</li>
    <li class="ui-state-default">L</li>
</ol>

This is my jquery code:

$(function() {
        $( "#selectable" ).click(function(){
            $(this).switchClass( "ui-state-highlight", 1000 );
            return false;   
        });
    });

For some reason I am having trouble understanding what I am doing wrong. I think I may be misunderstanding the meaning of ‘this’. I want the switchClass only to switch the class of whichever element I clicked on.

  • 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-06-09T06:29:51+00:00Added an answer on June 9, 2026 at 6:29 am

    What you want is this

    $(function() {
        // Use $( "#selectable > li" ) if you want to limit to direct children
        $( "#selectable li" ).click(function(){  //  binds the click event all li descendants of selectable
            $(this).switchClass("ui-state-default", "ui-state-highlight", 1000);  // this is now the li that was clicked
            return false;   
        });
    });
    

    better way would be to delegate as it’s more efficient by not binding an event handler to each li but having a parent element listen for the event as it bubbles up

    $(function() {
        $( "#selectable" ).on('click','li'(function(){  
            $(this).switchClass("ui-state-default", "ui-state-highlight", 1000);  // this is now the li that was clicked
            return false;   
        });
    });
    

    unless you want to click each #selectable which wouldn’t be valid since ID’s need to be unique. You can change it to a class and use

    $(function() {
        $( ".selectable" ).click(function(){  
            $(this).switchClass("ui-state-default", "ui-state-highlight", 1000);  
            return false;   
        });
    });
    

    You can use toggleClass like the following

    $(function() {
        $("#selectable").on('click', 'li', function() {
            $(this).toggleClass("ui-state-default ui-state-highlight"); // this is now the li that was clicked
            return false;
        });
    });​
    

    http://jsfiddle.net/G33Mc/

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

Sidebar

Related Questions

I have set of xml elements like this: <item code=<HTML_CODE> /> For example: <items>
I have a set of code like this: $('.rightMainP').hover(function() { $('#rightMain img:first-child').attr('src', '../Images/whiteSidewaysNub.png') },
I have a set of elements, each of which looks like this: <div class=maindiv>
I have a set of selected elements in jQuery: <div class=element></div> <div class=element></div> <div
I have a set of nested elements like such. <div id=master> <span id=num-1 class=num></span>
I have a set of elements with different attributes, like this: <?xml version=1.0 encoding=utf-8?>
I have set of 6 elements and I would like to filter by every
I have a set of lists that look like this: conditions = [ [condition1,
Is it possible to set an elements width like this: <img id=backgroundImg src=images/background.png alt=
I have a form set up like this in HTML: <input type=text name=data[type1][0] value=

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.