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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:49:40+00:00 2026-05-13T07:49:40+00:00

I have following code where commentTable is the class. It works fine for current

  • 0

I have following code where commentTable is the class. It works fine for current comments displayed on the page.

$(“.commentTable:odd”).addClass(“commentTableClass1”);
$(“.commentTable:even”).addClass(“commentTableClass2”);

but what I really want is that this addition of class be ‘live’ even when new comments are added. I don’t want to remove class and re-add them to get the effect. I just want to apply to new innerHTML the “correct” class.

I could keep count of number of comments and apply different class but I wanted something elegant.

  • 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-13T07:49:41+00:00Added an answer on May 13, 2026 at 7:49 am

    You could put them in a function and call the function each time…

    function oddEven(){
       $('.commentTable').removeClass('commentTableClass1').removeClass('commentTableClass2');
       $(".commentTable:odd").addClass("commentTableClass1");   
       $(".commentTable:even").addClass("commentTableClass2");
    }
    

    then you can just call oddEven(); rather than removeClass and addClass. It’s cleaner. I wouldn’t exactly call it elegant though.

    OR

    this would probably be faster and more what you’re looking for:

    function getCommentClass(){
       var newClass;
       if($('.comment').length % 2 == 0){
          //there's an even number already, the new one will be odd
          newClass = 'commentTableClass2';
       }else{
         //new one will be even
          newClass = 'commentTableClass1';
       }
    
       return newClass;
    }
    
    var commentClass = getCommentClass();
    $('#newElement').addClass(commentClass);
    

    Either way, though, you’d need to call a function each time you load a new comment.

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

Sidebar

Ask A Question

Stats

  • Questions 280k
  • Answers 280k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can get the desired result by specifying the desired… May 13, 2026 at 3:43 pm
  • Editorial Team
    Editorial Team added an answer You can prevent some malicious clients from hitting your server… May 13, 2026 at 3:43 pm
  • Editorial Team
    Editorial Team added an answer As an inner class inside your activity : public final… May 13, 2026 at 3:43 pm

Related Questions

I have the following code where I make an copy of my database that
I have the following code, where i am trying to create a generic collection
I have the following code, where ApplicationType is an enum. I have the same
I have the following code snippet where some strings are initialized in the if
I have the following Javascript code, where feed is a valid url to an

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.