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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:01:49+00:00 2026-05-14T15:01:49+00:00

I have a table cell in the footer that allows the user to turn

  • 0

I have a table cell in the footer that allows the user to turn on row coloring:

$('#highlight').click(function() {
$(this).parents('table').RowColors();
})

// From Chapter 7 of Learning jQuery
$.fn.RowColors = function() {
$('tbody tr:odd', this).removeClass('even').addClass('odd');
$('tbody tr:even', this).removeClass('odd').addClass('even');
return this;
};

Q: How do I write a selector that says: IF there is at least 1 row with class=”even”, then remove both “even” and “odd” ELSE execute the RowColors function.

  • 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-14T15:01:50+00:00Added an answer on May 14, 2026 at 3:01 pm

    My advice would be to do it slightly differently. Have just one class with the default state being the other. So:

    tr td { background: yellow; }
    tr.odd td { background: blue; }
    

    and then this is as simple as:

    $("tr").toggleClass("odd");
    

    or more specifically:

    $("tbody > tr").removeClass("odd").filter(":nth-child(odd)").addClass("odd");
    

    Note: avoid using :odd and :even. They usually don’t mean what you think they mean. :nth-child(odd) and :nth-child(even) tend to be what you really mean.

    I would probably write something like:

    $("#highlight").click(function() {
      $(this).closest("table").children("tbody").children("tr").removeClass("odd")
        .filter(":nth-child(odd)").addClass("odd");
      return false;
    });
    

    Put it into a separate function if you wish.

    Edit: to check whether something is empty:

    var odd = $(".odd");
    if (odd.length == 0) {
      // do one thing
    } else {
      // do something else
    }
    

    jQuery objects support the length property and the size() method, which do the same thing.

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

Sidebar

Related Questions

I have a table cell of type nvarchar(max) that typically looks like this: A03
I have a table cell (td) that triggers a script when clicked. I have
I have a table cell that isn't entirely filled with text so the bg
I have a table cell, and when the user clicks on it, I replace
I have a div inside a table cell. When I click in the table
I have a class A that inherits UITableViewCell (to customize a table cell). It
I have a table that looks like this in a usercontrol. Then the usercontrol
I have a table cell with a div that has a height of 3px
I am trying to figure out how to have a table cell highlight when
I have this table cell, to which I'm appending a radio button through jQuery.

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.