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

The Archive Base Latest Questions

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

My toggle function is only toggling through 2 of the 3 classes and not

  • 0

My toggle function is only toggling through 2 of the 3 classes and not the third. It skips my star class and just toggles between my fact and barr classes. I’ve changed things around a bunch of ways and it’s only toggling through 2 of 3 classes.

My Html

<div id="labalt"><a href="#">Alt</a></div>
  <h1 id="build_title" class="barr" >Barracks</h1>

My code

$("#labalt").click(function() {
    $(this).toggleClass("fact star barr");

if($(this).hasClass("fact"))
    {
    $("#build_title").html("Factory");
    }
else if($(this).hasClass("star"))
{
    $("#build_title").html("Starport");
    }
else
{
    $("#build_title").html("Barracks");
    }
 });
  • 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-09T07:08:31+00:00Added an answer on June 9, 2026 at 7:08 am

    Are you trying to get the code to cycle through all three texts on three consecutive clicks? Because that’s not what toggleClass does. toggleClass toggles one or more classes on or off.

    As you can tell by this fiddle, clicking the link toggles both the border, the font size, and the italics class.

    It starts off with none of the classes. On click 1, it will have all three classes. It will then match the first condition (it has the fact class), and since they are else if‘s, no other conditions will be checked. The text will simply be set to “Factory”.

    On a second click, all three classes will be removed. It will not meet the first criteria (fact) or the second (star), so it will set the text to “Barracks”.

    On a third click, all three classes will be added again.

    If you want it to cycle through the classes, you’d have to implement that manually; I do not know that there is any out of the box solution for that. You would either need to keep a cursor of some kind, or continuously iterate an array to check whichever class is currently active, and then move on to the next. Example; I’m sure it could be neater.

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

Sidebar

Related Questions

I need to toggle an element ONLY if it is not disabled. jQuery(#sbutton).toggle( function
I am trying to make a toggle function work, but since I have only
I have the following code: $('a.btn-slide').toggle(function() { $(#SliderDIV).slideDown(fast); $(this).text('Hide/Show'); }); Only in Firefox, when
Have two events: $('body').mouseup(function(e){} and $('.toggle').click(function(e){} I only want one of these to trigger.
I'd like to execute a JQuery toggle function (it toggles an input field to
Please I need to apply the toggle() function to only li that has ul
i have a toggle function which i have working fine, only i cant get
I have this situation: $(#button).toggle(function(){ $(#window).animate({top:'0%'},1000); },function(){ $(#window).animate({top:'-100%'},1000); }); but I need change it
I'm using the jQuery toggle function to change some characters of an text input.
I try to use the toggle function of the Bootstrap collapse plugin programmatically. I

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.