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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:20:36+00:00 2026-05-23T06:20:36+00:00

Hi I have this following code with jquery , so it will alternate colors

  • 0

Hi I have this following code with jquery , so it will alternate colors on each row of a table:

  $("tr").filter(':nth-child(2n+1)').addClass('odd').next().addClass('even');  

but unfortunately it doesnt work when the table comes from an ajax call .. is there a way to put this into a $(“tr”).live… and make it work like that? .. I have tried different aproaches like this: ( I know the syntax may not be correct ).

$("tr").live({ 
 $("this").filter(':nth-child(2n+1)').addClass('odd').next().addClass('even')
});  

But doestn seem to work

  • 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-23T06:20:36+00:00Added an answer on May 23, 2026 at 6:20 am

    live isn’t a catch-all solution for problems that come from dynamic content. It only works with events. Normally, you listen for events with bind; when your content is dynamic, live is a way to capture events reliably. So you need another solution for styles…

    The simplest solution is to define your CSS using nth-child:

    tr:nth-child(2n+1) {
        // whatever the odd styles are
    }
    
    tr:nth-child(2n) {
        // whatever the even styles are
    }
    

    This won’t work in all browsers, since it’s CSS3. (In particular, it won’t work in Firefox 3 or Internet Explorer before version 9.)

    The other solution is to update all the tr elements on the page after every AJAX call using ajaxComplete:

    $(document).ajaxComplete(function() {
        $('tr:nth-child(2n+1)').addClass('odd').next().addClass('even');
    });
    

    This won’t have great performance, but it probably won’t be a particular drain as it will always happen asynchronously. This will, obviously, work cross-browser.

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

Sidebar

Related Questions

I have the following jquery code: jQuery(function(){ jQuery(select#rooms).change(function(){ var options = ''; jQuery.getJSON(/admin/selection.php,{id: jQuery(this).val(),
I have problems with the following bit of javascript/jquery code: this.droppable = function(){ $('.imageWindow
I have the following jQuery code $(#dropdown).hover(function() { $(this).stop(true,true).fadeTo('fast',1); $(#options).stop(true,true).slideDown(); }, function() { $(this).delay(1000).stop(true,true).fadeTo('fast',0.1);
I have the following jQuery code for finding the sum of all table rows,
I'd like some help with the following jQuery code if possible... $(document).ready(function() { $('table.sortable').each(function()
Have a look at the following code (additionally, you will need jquery.js , jquery.viewport.js
I have the following code: css: nav div:nth-child(1) { background: red; } nav div:nth-child(2)
this is my first question here. I have the following jquery code: $(document).ready(function(){ $(a).click(function(){
Let's assume you have a table and the following Jquery code var rows =
I have the following jQuery Code: $(#menu span).click(function() { var url = this.getAttribute(data-url); var

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.