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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:41:28+00:00 2026-05-28T03:41:28+00:00

I currently have a html table (with 2 columns) and I am trying to

  • 0

I currently have a html table (with 2 columns) and I am trying to add various effects to this table such as table row hover effect, table click effect, etc…This is the script I am using now and its working fine so far:

<head>
<script type="text/javascript"> 
$(document).ready(function(){   

    //the following script is to change the table row bg color when clicked
    $('tr').click(function(){
    $('tr td').css({ 'background' : 'none'});
    $('td', this).css({ 'background-color' : '#CCC' });
  }); 

    //the following script is for the table row hover effect
    $('tr').hover(function() {
            $(this).css('background-color', '#FFFF99');
        },
        function() {
            $(this).css('background-color', '#FFFFFF');
            $(this).contents('td').css('border', 'none');
        });


});  
</script>

</head>

<body>
<table>
<tr>
<td> <a href="#">Movie Part 1</a></td>
<td>01:23:26</td>
</tr>

<tr>
<td> <a href="#">Movie Part 1</a></td>
<td>01:23:26</td>
</tr>

</table>

</body>

From the above template, you can see that I am having 2 columns for this table and 1 columns has the tag with the href link and the other column doesnt have any tag.

Like I mentioned above, the script I am using to highlight the table row when clicked works. But I am trying to slightly modify the script so that it only execute the click effect only when the “a” tag inside the “tr td” is clicked. Right now, the table row is highlighted doesnt matter where I click inside the ‘tr’ area. It also highlights even if I click the second column inside the ‘tr’ which has no link.

How can I modify this above script so it highlights the entire table row ONLY when the ‘a’ tag inside the table row’s first column is clicked?

  • 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-28T03:41:28+00:00Added an answer on May 28, 2026 at 3:41 am

    jQuery 1.7.x :

    $('tr').on('click','a', function () {
        $(this).closest('tr').css({ 'background-color' : '#ccc'});
    }); 
    

    I’m thinking that you only need to change the background color of the TR and not the TD. If I’m mistaken, just use this instead:

    $('tr').on('click','a', function () {
        $(this).closest('tr').children('td').css({ 'background-color' : '#ccc'});
    });
    

    jQuery 1.6.x and below :

    $('tr').delegate('a', 'click', function () {
        $(this).closest('tr').children('td').css({ 'background-color' : '#ccc' });
    });
    

    EDIT

    As per your comment below, you want to revert a row back to a normal background color when another row is clicked.

    To do that efficiently, I’d suggest you resort to CSS rules. Heck, come to think of it, we should have done that even as the question was first answered.

    First, define CSS rules like so:

    tr { background-color:#fff; /* or your default */ }
    tr.selected { background-color:#ddd; /* or your selected color */ }
    

    then switch that using jQuery:

    $('tr').delegate('a', 'click', function () {
        $(this)
            .closest('tr').addClass('selected')
            .siblings('tr').removeClass('selected')
            ;
    });
    

    If just modifying the tr does not work for you, modify your CSS rules like so:

    tr td { background-color:#fff; /* or your default */ }
    tr.selected td { background-color:#ddd; /* or your selected color */ }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an HTML table displaying a lot of columns in a single row
I have an html table with many rows. I'm currently grouping several rows inside
I have this command that I run every 24 hours currently. find /var/www/html/audio -daystart
I have a html table with CSS. Currently all the cells have a white
I have a html table which consists of columns for days, and rows for
I currently have a table that is 17 columns wide and has 30 records.
I have a dynamic table with each row having 4 columns. Column 1 has
I have an html table that is displaying customer orders. One of these columns
I have an HTML table whose cells contain, among other things, spans, like this:
I have a large report currently rendered as a regular HTML table. I'd like

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.