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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:02:36+00:00 2026-05-22T18:02:36+00:00

addClass doesn’t seem to work under certain circumstances for me. Have a look at

  • 0

addClass doesn’t seem to work under certain circumstances for me. Have a look at the following code:

$("#my-div-container .my-sub-div li a").live('mouseover', function(e){
    e.preventDefault();

    var li_parent_elem = $(this).parent();
    li_parent_elem.siblings().removeClass();

    switch(true)
    {
        case (li_parent_elem.is("#my-div-container .my-sub-div li:first-child") == true):
            //alert("First");
            li_parent_elem.addClass("first");
        break;

        case (li_parent_elem.is("#my-div-container .my-sub-div li:last-child") == true):
            //alert("Last");
            li_parent_elem.addClass("last");
        break;

        default:
            //alert("Mid");                     
            li_parent_elem.addClass("mid");
    }
});

addClass doesn’t work for the first two case statements, only the default. However, when I uncomment the alert directives, I get the correct alert so I know the conditional statement is working.

I’m not sure what is wrong here, and I’d appreciate some assistance.

  • 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-22T18:02:37+00:00Added an answer on May 22, 2026 at 6:02 pm

    After a bit of fiddling I seem to have found a solution to this, even though I don’t really like it because I don’t understand why it works and the earlier code doesn’t.

    Here’s the modified code:

    $("#my-div-container .my-sub-div li a").live('mouseover', function(e){
    e.preventDefault();
    
    var li_parent_elem = $(this).parent();
    li_parent_elem.siblings().removeClass();
    
    switch(true)
    {
        case (li_parent_elem.is("#my-div-container .my-sub-div li:first-child") == true):
            //alert("First");
            li_parent_elem.removeAttr("class").addClass("first");
        break;
    
        case (li_parent_elem.is("#my-div-container .my-sub-div li:last-child") == true):
            //alert("Last");
            li_parent_elem.removeAttr("class").addClass("last");
        break;
    
        default:
            //alert("Mid");                     
            li_parent_elem.addClass("mid");
    }
    });
    

    I’m still at a loss why even when I removed the switch statement and used if-else with my earlier code, it didn’t work, but this does.

    Maybe it’s a bug or something.

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

Sidebar

Related Questions

Something as simple as: $(#div).addClass(error).delay(1000).removeClass(error); doesn't seem to work. What would be the easiest
I have the following code: $(#submenuSolutions).mouseleave(function () { $(.submenuRightCurrent).hide(); $(.submenuRightCurrent).removeClass(submenuRightCurrent); $(#prasentRight).addClass(submenuRightCurrent); $(#prasentRight).addClass(submenuRightCurrent).stop(true,true).show(); $(.menuHover).removeClass(menuHover); $(this).stop(true,true).hide();
I have these two lines: $target_box.children('a.ajax_trigger_title').addClass('opened_post_title'); jQuery('#'+$target_box.attr('id')+' a.ajax_trigger_title').addClass('opened_post_title'); The first line doesn't work, but
$(.m).toggle( $(.m).addClass('on'), $(.m).removeClass('on') ); This doesn't seem to work the way I want it
I have the following jQuery calls chain: $(someSelector).nextUntil(.specialClass).addClass(classBasedSomeSelectorObject) I need to addClass that's value
I am using the following jquery code: $(#top ul li.corner).mouseover(function(){ $(span.left-corner).addClass(left-corner-hover); $(span.right-corner).addClass(right-corner-hover); $(span.content).addClass(content-hover); }).mouseout(function(){
I have the following jQuery: $(#toolbar).hover(function () { $(.logo).fadeOut(slow, function () { $(this).addClass(logohover) $(this).parent().addClass(logohover).fadeIn(slow,
The below code works fine in Chrome, but doesn't work correctly in Firefox. In
I want to do an addclass, but only if it doesn't have a child
This code doesn't work as intended, what am I doing wrong? $(document).ready(function(){ $(a.hide-para).click(function(){ $('p').hide();

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.