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 7758255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:18:42+00:00 2026-06-01T13:18:42+00:00

I want to toggle a class based on some condition which I have to

  • 0

I want to toggle a class based on some condition which I have to compute from the ID of the processed element:

$(".centre li").toggleClass("highlight", someFunction(x));

(This will select about 10 list elements. I need each element’s ID to look up if it requires highlighting or not.)

someFunction(selectedElement) {
  if (selectedElement.id in someArray)
    return true
  else
    return false
}

I might have lost my way in closure-hell, but this in the function is set to another element. This is the real code:

...
showListItem = function(linkSelector, listSelectors, contentSelector) {
return function() {
    $(".centre li").toggleClass("backgroundFullOrange", test(this));
    function test(a) {
        console.log($(a).attr("id")); // -> id of the linkSelector-element
    }
}
};  
$(linkSelector).bind('click', showListItem(linkSelector, listSelectors, contentSelector))
...

Update: turned out that toggleClass() was not the function I’ve been looking for. See accepted answer for correct usage.

  • 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-01T13:18:43+00:00Added an answer on June 1, 2026 at 1:18 pm

    First, $(".centre li").toggleClass("highlight", someFunction(x)); is not a correct syntax. The 2nd arg is switch and the definition is as below,

    switchA boolean value to determine whether the class should be added
    or removed.

    switch cannot be a function as you have.

    In your case you going to have to call .removeClass('highlight') and then addClass inside which you can compare the ID of the element and return highlight or ”.

    See DEMO which randomly highlights 2 li’s every 1.5 secs.

    DEMO

        $(".centre li")
           .removeClass('highlight')
           .addClass(function () {
               return ($.inArray(this.id, someArray) >= 0)?'highlight': '';
           });
    

    I think I understood it correctly this time. See below,

    DEMO

    $(function() {
        var someArray = ['l1', 'l8'];
    
        $(".centre li").toggleClass(function() {
            return ($.inArray(this.id, someArray) >= 0)?'highlight':'';
        });
    
    });
    

    Try using .filter. Something like below,

    $(".centre li").filter(function() {
        return $.inArray(this.id, someArray);
    }).toggleClass("highlight");
    

    The highlight will be added only to those elements that are returned by the filter.

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

Sidebar

Related Questions

I have a scenario where I want to toggle series based on checkbox selection.
I want to toggle two radio buttons and select fields based on which radio
Suppose I have this class: template</*some other parameters here */class toggle> class Foo {
ok, So I have multiple divs(7) which I want to toggle for show hide.
I want to toggle a process's visibility at runtime , I have a Windows
I have a div that I want to toggle between displaying or not when
I have an html special character I want to toggle off and on like
I am working on an app in which I want to toggle the status
Overview From time to time in jQuery I want to check a condition of
I want to toggle a class of a DIV by clicking on it, but

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.