Clear the styling of a bootstrap table to remove any highlighting

Check out an example with a table here

I am looking to eliminate the highlighting from a row when it is clicked on.

Currently, when hovering over a row, the background becomes grey. I want the ability to remove the grey background by clicking on the row so that it remains without the set background.

Is there a way to achieve this?

I have created a function that selects the cell and row that are being clicked:

var test = function(element){
    var tdCaller = $(element);
    var parentRow = tdCaller.parent();
    var parentBody = parentRow.parent();
}

However, I am unsure of how to remove the highlighting from the row upon click.

Answer №1

Make the background of the clicked row none.

$(document).ready(function(){
  $('.table-hover tbody tr').click(function(){
    $(this).css('background', 'none'); // Pick any color you like
  })
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel=stylesheet href=https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.6/css/bootstrap.min.css>
<table id="tblBAENWC" class="table table-stripped table-bordered table-hover table-condensed col-sm-12">
  <thead>
    <tr>
      <th>ID</th>
      <th>Name</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td style="cursor:pointer;">1</td>
      <td style="cursor:pointer;">First name</td>
    </tr>
    <tr>
      <td style="cursor:pointer;">1</td>
      <td style="cursor:pointer;">First name</td>
    </tr>
    <tr>
      <td style="cursor:pointer;">1</td>
      <td style="cursor:pointer;">First name</td>
    </tr>
    <tr>
      <td style="cursor:pointer;">1</td>
      <td style="cursor:pointer;">First name</td>
    </tr>
  </tbody>
</table> 

Answer №2

To keep changes to your code minimal, I have made a slight adjustment:

You can access the link here.

Html (added class on tr):

<table id="tblBAENWC" class="table table-stripped table-bordered table-hover table-condensed col-sm-12">
  <thead>
    <tr>
      <th>ID</th>
      <th>Name</th>
    </tr>
  </thead>
  <tbody>
    <tr class="mytr">
      <td style="cursor:pointer;" onclick="test(this);">1</td>
      <td style="cursor:pointer;" onclick="test(this);">First name</td>
    </tr>

    <tr class="mytr">
      <td style="cursor:pointer;" onclick="test(this);">1</td>
      <td style="cursor:pointer;" onclick="test(this);">First name</td>
    </tr>

    <tr class="mytr">
      <td style="cursor:pointer;" onclick="test(this);">1</td>
      <td style="cursor:pointer;" onclick="test(this);">First name</td>
    </tr>

    <tr class="mytr">
      <td style="cursor:pointer;" onclick="test(this);">1</td>
      <td style="cursor:pointer;" onclick="test(this);">First name</td>
    </tr>
  </tbody>

</table>

Javascript (look after comment "my add"):

var test = function(element) {

    var tdCaller = $(element);
    var parentRow = tdCaller.parent();
    var parentBody = parentRow.parent();

    // my add
    $('tr.mytr.clicked').removeClass('clicked')
    parentRow.addClass('clicked');
  }

CSS (look after comment "my add"):

.MarkSelectedRow {
  background-color: cornflowerblue;
}

/* my add */
.table-hover tbody tr.mytr.clicked {
  background-color: white;
}

Answer №3

Utilize this code snippet for a useful function.

$(function(){
  $('.table-hover tr').on('click', function(){
    $(this).addClass('no-hover');
  });
});
/* Styling for table row hover */

.table-hover tr:hover td{
  background-color: rgba(0, 0, 0, 0.85);
  color: #ffffff;
}
/* Changes after clicking on a row and hovering again */
.table-hover tr.no-hover:hover td{
  background-color: #ffffff;
  color: #333333;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>

<table class="table table-hover">
  <thead>
    <tr>
      <th>Table Head 1</th>
      <th>Table Head 2</th>
      <th>Table Head 3</th>
      <th>Table Head 4</th>
      <th>Table Head 5</th>
    <tr>
  </thead>
  <tbody>
    <tr>
      <td>Table cell content 1</td>
      <td>Table cell content 2</td>
      <td>Table cell content 3</td>
      <td>Table cell content 4</td>
      <td>Table cell content 5</td>
    </tr>
    <tr>
      <td>Table cell content 1</td>
      <td>Table cell content 2</td>
      <td>Table cell content 3</td>
      <td>Table cell content 4</td>
      <td>Table cell content 5</td>
    </tr>
    <tr>
      <td>Table cell content 1</td>
      <td>Table cell content 2</td>
      <td>Table cell content 3</td>
      <td>Table cell content 4</td>
      <td>Table cell content 5</td>
    </tr>
    <tr>
      <td>Table cell content 1</td>
      <td>Table cell content 2</td>
      <td>Table cell content 3</td>
      <td>Table cell content 4</td>
      <td>Table cell content 5</td>
    </tr>
    <tr>
      <td>Table cell content 1</td>
      <td>Table cell content 2</td>
      <td>Table cell content 3</td>
      <td>Table cell content 4</td>
      <td>Table cell content 5</td>
    </tr>
  </tbody>
</table>

Answer №4

Styling with CSS is the way to go. Include this code in your CSS stylesheet:

tr:hover td {background:#fff;}

You can easily change the background color on hover without using jQuery - just use CSS.

To make the row's color white when clicked, add the following JavaScript code:

 $(document).ready(function () {
        $('tr').click(function () {
            //Check if the background color is set or already white
                      $(this).css('background', 'white');


      });
});

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

Error in Jquery click event not detecting the correct value

My function is designed to add or remove items from a SQL database upon click. I use a condition to differentiate between adding and removing, but while the add function works perfectly, the remove function does not, even though they share the same code. ...

Form input using the div element (when clicked)

I am currently working on a page that connects to a database with user information. For each user, I am creating a separate div element so that there are 6 divs total (each representing a different user). My goal is to have the ability for a user to click ...

Adjust the height of taller images to match the size of other images within a flexbox grid

On my Wordpress category page, I have a flexbox grid showcasing posts with titles and excerpts. Most images have the same proportions, but a few are significantly taller. I'm looking to responsively set the max-height of the tall images to match the h ...

Creating consistent height list groups within a Bootstrap 4 card deck

How can I create a card-desk using Bootstrap 4 that contains internal list-groups or card-blocks with the same horizontal height without setting a fixed height for each section? I have tried using d-flex flex-column and list-groups, but it didn't wor ...

Having trouble parsing JSON with Ajax in Pusher using PHP?

I am facing an issue while sending multiple parameters using the Pusher AJAX PHP library. This is the error I encounter: SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data Here is my PHP and JS code: <script src="https: ...

Silence from PHP script after executing jQuery post() method

I've run into an issue while trying to retrieve data from a PHP file that reads a mySQL database. The data is delivered successfully when accessed through a browser: However, when attempting to access the data using jQuery's get() or post() meth ...

Tips on how to align a wrapper-div in the center without affecting the positioning of the

I am looking to keep my page perfectly centered in the browser without affecting the content (similar to how align-text: center works). Specifically, I want to center my wrapper-div. How can I achieve this? Here is a simplified version of my current page ...

Assess html code for Strings that include <% %> tags along with embedded scripts

Having a small issue with my code where I am receiving an HTML response from a web service as a java String. I need to display this String as HTML on my webpage, but the problem is that there are some script tags in the form of <% ... %> which are sh ...

Interacting with various cookies created from user-provided input (specifically from textboxes) simultaneously

I'm facing a challenging problem and I'm in need of some assistance. The task at hand is to create three text boxes for users to input values for cookies named: name, city, and hobby. Then, using a single button with an onclick event, a function ...

Deactivate Link Using CSS while Allowing Title to be Functional

Is there a way to enable the link in CSS while still showing the title? Take a look at my code: CSS and HTML Code: .disabledLink { pointer-events: none; opacity: 0.6; cursor: default; } <a href="www.google.com" class="disableLink" title="M ...

Arrange the footer content into multiple columns

Hello! I'm currently working on creating a footer for my website. I've written this code and everything seems to be functioning correctly, except for the fact that the second row is positioned under the first row and taking up half of the bottom ...

Leveraging AJAX for sending information to an API

A script I've written utilizes AJAX to communicate with a PHP-based API. The first part of the script is responsible for loading trade history: $(document).ready(function () { var orders = $('#History ul'); ...

Tips for enhancing redundancy in HTML and ROR using partials

Looking for a way to merge two partials in my Ruby on Rails app without copy-pasting. Here's what I'm aiming for: @member = 'Player' render 'team/team_partial' @member = 'Staff' render 'team/team_partial&a ...

Add a dynamic widget to a specific element in the document object model in real time

Is there a way to dynamically create a jQuery UI widget using a string? You can easily do this by following the example below: $(function() { $("selector").widget() }) However, what I am trying to accomplish is slightly different. Here is an examp ...

What is the best way to create a seamless change from a black to white invert filter?

I'm currently working on a project where I need to change my icons from black to white. After some research, I found that the best way to do this is by using filter: invert(100%). However, when I try to transition it, the change happens abruptly after ...

Issue with HighCharts: Bar columns not extending to the x-Axis when drilling up

I am encountering an issue with HighChart/HighStock that I need help with. To illustrate my problem, I have set up a JSFiddle. The problem arises when a user drills down on a bar column, causing the y-axis to shrink and consequently making the x-axis appea ...

Mastering the Correct Handling of HTTP Status Codes

I'm having an issue with the new feature in http://api.jquery.com/jQuery.ajax where my 201 and 202 callbacks are being ignored. This problem is occurring with Firefox 4.0_b10 and Chromium 9.0. I am eager to find a solution to this small problem. Bes ...

Create a CSV document using information from a JSON dataset

My main goal is to create a CSV file from the JSON object retrieved through an Ajax request, The JSON data I receive represents all the entries from a form : https://i.sstatic.net/4fwh2.png I already have a working solution for extracting one field valu ...

Make sure to incorporate the Readme.md file into your HTML document

I am currently managing a static HTML page on my personal server. The README.md file for this project is stored in a public GitHub repository. My goal is to have the contents of the ReadMe automatically included on my local HTML page. Essentially, I am lo ...

Various arrangements of rows and columns based on screen dimensions (bootstrap)

I am looking to customize the row and column layout based on the screen size. For example, currently for medium screens, I have the following layout: <div class="row align-items-md-center"> <div class="col-6">1</div> ...