Guide to altering the color of the row that is clicked in a table using css and jquery

Is there a way to change the text color of a row in a table when it is clicked, similar to an example like this but without changing the background color? The code I have tried is not working for me.

Below is a snippet of the code I am using:

$("#myTable tr").on('click', function() {
    $(this).addClass("done");
});
table {
    margin: 0 auto;
    border: 1px solid white;
}
tr td {
    padding: 8px 8px 8px 8px;
    color: #2C3D50;
    font-weight: 600;
}
table td .fa {
    font-size: 1.3em;
}

.member > tr > td:nth-child(1) {
    border-right: none;
}

td:hover {
    background-color: #00BD9A;
    color: white;
}
    
tr .done{
    color: #F7AA25;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="myTable">
    <colgroup>
        <col style="width:70%">
        <col style="10%">
        <col style="10%">
        <col style="10%">
    </colgroup>
    <tbody>
        <tr>
            <td>Lorem ipsum dolor sit amet <i class="fa fa-eye pull-right" aria-hidden="true"></i></td>
            <td><i class="fa fa-share-alt" aria-hidden="true"></i></td>
            <td><i class="fa fa-download" aria-hidden="true"></i></td>
            <td><i class="fa fa-envelope-o" aria-hidden="true"></i></td>
        </tr>
        <tr>
            <td>Pellentesque in felis <i class="fa fa-eye pull-right" aria-hidden="true"></i></td>
            <td><i class="fa fa-share-alt" aria-hidden="true"></i></td>
            <td><i class="fa fa-download" aria-hidden="true"></i></td>
            <td><i class="fa fa-envelope-o" aria-hidden="true"></i></td>
        </tr>
    </tbody>
</table>

Answer №1

Make the necessary changes to your jQuery code as shown below.

  1. Make sure to eliminate the selected class from all rows -
  2. Include the selected class in the current/selected row.
$("tbody tr").click(function() {
  console.log('clicked');
  $("tbody tr").removeClass('selected');
  $(this).addClass('selected');
});

Here is the updated Fiddle.

Answer №2

Discover the solution to locate your td elements and apply the CSS class .done without using tr .done

$("#myTable tr").on('click', function() {
    $(this).find('td').addClass("done");
});

$("#myTable tr").on('click', function() {
    $(this).find('td').addClass("done");
});
table {
    margin: 0 auto;
    border: 1px solid white;
}
tr td {
    padding: 8px 8px 8px 8px;
    color: #2C3D50;
    font-weight: 600;
}
table td .fa {
    font-size: 1.3em;
}
.member > tr > td:nth-child(1) {
    border-right: none;
}

td:hover {
    background-color: #00BD9A;
    color: white;
}

.done{
    color: #F7AA25;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table id="myTable">
    <colgroup>
        <col style="width:70%">
        <col style="10%">
        <col style="10%">
        <col style="10%">
    </colgroup>
    <tbody>
        <tr>
            <td>Lorem ipsum dolor sit amet <i class="fa fa-eye pull-right" aria-hidden="true"></i></td>
            <td><i class="fa fa-share-alt" aria-hidden="true"></i></td>
            <td><i class="fa fa-download" aria-hidden="true"></i></td>
            <td><i class="fa fa-envelope-o" aria-hidden="true"></i></td>
        </tr>
        <tr>
            <td>Pellentesque in felis <i class="fa fa-eye pull-right" aria-hidden="true"></i></td>
            <td><i class="fa fa-share-alt" aria-hidden="true"></i></td>
            <td><i class="fa fa-download" aria-hidden="true"></i></td>
            <td><i class="fa fa-envelope-o" aria-hidden="true"></i></td>
        </tr>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to access the child component in React?

const navArr = [ { path: "/introduction", title: "회사소개", subTitle: [{ title: "summary" }, { title: "vision" }], }, ] {navArr.map((obj) => { return ( <NavItem> ...

I am having trouble dealing with an integer AJAX response because when I try to alert it, all I get is [object, object]

$.ajax({ type: "POST", url: url_delete_admin_privilidge, data: postData, success: function(response) { console.log(response); setAdminResponse(response); alert("success") ...

Error occurs when using jQuery.ajax on mobile devices such as android or ios

When accessing my website, an ajax request is made to my REST API. It works perfectly on desktop browsers like Chrome, Internet Explorer, and Firefox, as well as on my Windows phone. However, when trying to access it from an Android or iOS device, the XHR ...

IE8 experiencing issues with colgroup tag

Having trouble styling a data table with alternating row colors. 1) Need help applying alternating row style without having to add classes to each individual TD? 2) Colgroup works in IE8 but having alignment issues for specific columns (cols=A&SI Cap ...

Triggering the hidden radio button with an OnClick event

I am using the Bootstrap framework to design my user interface. I have a specific requirement where I need to display multiple buttons that function as radio buttons. <div class="btn-group" data-toggle="buttons"> <label class="btn btn-primary a ...

Watching for changes to an object's value in an AngularJS service triggered by a controller from a separate module (Extended Edition)

Referring to this discussion on Stack Overflow: AngularJS trigger and watch object value change in service from controller The original question was about watching for changes in a service from a controller. I am interested in extending this concept to ...

Refresh the copyright year by syncing with a time server

Not long ago, I found myself needing to update the copyright year on a website. Instead of using JavaScript to get the year from the system time, I began wondering if there was a way to utilize an internet time server for this purpose. I believe that util ...

Retrieving information from a separate JavaScript file

I'm currently developing a Discord Bot and my code is all contained within one file. My goal now is to break this code up into multiple files for better organization. For instance, I plan to have: index.js which will handle all the requires (e.g. var ...

Evaluation of Library (VueJS) - Displaying various components in an individual test case

Just starting out with testing and have a simple question: I am working on testing a checkbox component. I understand the basics, but how can I render multiple components within one it block? Here is my current code. I am stuck on the second test where I ...

Execute Cheerio selector once the page has finished loading

Hey there! I'm trying to extract the URL value of an iframe on this website: I've checked the view page source but couldn't find the iframe. Looks like it's loaded after the page is fully loaded through JavaScript. Could it be that ...

Chart showing a Google Timeline: Allow for each bar to be colored differently when there are multiple bars on the same line

It appears that the Google Timeline charts have a feature to color individual blocks on the timeline as indicated in the documentation at However, there seems to be an issue when two bars overlap on the same line, which is evident in this fiddle: http://j ...

Can someone explain to me how this AngularJS factory example functions? I have some uncertainty

As a beginner in AngularJS, I am currently studying it through a tutorial and have some questions regarding the use of the factory in Angular. From what I understand, a factory is a pattern that creates objects on request. In the example provided, we see ...

What is the best method for adding a line break in the body of an email when we include the recipient, subject, and message?

Do you have a requirement where information needs to be sent to an end user via Gmail (preferred) and they need to click on a button in the email body to respond? The button response is set up with predefined to, subject, and body fields, but there are dif ...

Using values from an appsettings.json file as href variables in an AMP html: a complete guide

Currently developing an AMP website with the URL www-dev.example.com, directing users to a sign-up page. The sign-up page is hosted on an identity server under the subdomain auth.www-dev.example.com. Now, the goal is to deploy the AMP website to a staging ...

Tips for comparing props in the ComponentDidUpdate method when dealing with a complex data structure that is connected to Redux

I have been experimenting with the new lifecycles of React v16. It works perfectly fine when comparing single keys. However, when dealing with large data structures like Arrays of objects, performing deep comparison can be quite expensive. My specific sce ...

Creating keys for my console.log data and appending it to html in order to display console log results in textboxes

I am currently developing a matching system for Player vs. Player battles and I need to ensure that the keys are appended correctly to my div element. Previously, I have successfully used append with keys before. Below is the code snippet: ...

Determine the presence of a Facebook user by using JavaScript

I am trying to find a method to verify the existence of a Facebook user based on their ID. As far as I understand, there is a Graph API that can provide a Facebook user's profile picture using their ID in this format: I have noticed that if an inval ...

Creating HTML controls dynamically based on each row in a table using ASP.NET MVC

I am currently working on an application using Asp.Net MVC3 and I need to create a view that reflects the data in my database table. For example, if my table has one column with 5 records (rows), I want my application's view to display those 5 record ...

Enhance Your Navbar in Bootstrap 3 by Creating Vertically Expanding Links Relative to Brand Image Dimensions

To best illustrate my issue, I will begin with a screenshot of the problem and then explain what I aim to achieve. Here is the current state of my navbar: Currently, the text of the links on the right side is positioned at the top. Additionally, this is h ...

:Incorporating active hyperlinks through javascript

Hey there, I've encountered a little conundrum. I have a header.php file that contains all the header information - navigation and logo. It's super convenient because I can include this file on all my pages where needed, making editing a breeze. ...