Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<tr> background color only changes on even childs

Try to click on any table row, it will only black highlight the even ones, what did I miss?
It was supposed to turn row backgrounds black, on click.
Here's my current code:
HTML:

<div class="table-responsive">
    <table id="tEmprego" class="table table-striped table-hover">
      <thead>
        <tr>
          <th>#</th>
          <th>Table heading</th>
          <th>Table heading</th>
          <th>Table heading</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>1</td>
          <td>Table cell</td>
          <td>Table cell</td>
          <td>Table cell</td>
        </tr>
        <tr>
          <td>2</td>
          <td>Table cell</td>
          <td>Table cell</td>
          <td>Table cell</td>
        </tr>
        <tr>
          <td>3</td>
          <td>Table cell</td>
          <td>Table cell</td>
          <td>Table cell</td>
        </tr>
        <tr>
          <td>4</td>
          <td>Table cell</td>
          <td>Table cell</td>
          <td>Table cell</td>
        </tr>
      </tbody>
    </table>
  </div>

CSS:

.table-hover>tbody>tr:hover>td, .table-hover>tbody>tr:hover>th {
  background-color: #550055;
  color:#eeeeee;
}

.hovered{
   background-color: #000000;
  color:#ffffff;
}

Javascript:

$("#tEmprego").on('click','td',function() {
       $(this).closest("tr").siblings().removeClass("hovered");
       $(this).parents("tr").addClass("hovered");
});

Bootply:
http://www.bootply.com/28I0IItFmP

like image 232
BernardoLima Avatar asked Aug 26 '14 19:08

BernardoLima


People also ask

Which CSS would correctly apply the background color to every odd row in your table?

Tap into the tr (table row) element is CSS. Use the nth-child() selector and add background-color of your choice to all odd (or even) table rows.

Does background color inherit?

Background properties do not inherit, but the parent element's background will shine through by default because of the initial 'transparent' value on 'background-color'.

Can TR have background color?

HTML | <tr> bgcolor AttributeThe HTML <tr> bgcolor Attribute is used to specify the background color of a table row. It is not supported by HTML 5. Attribute Values: color_name: It sets the background color by using the color name.

Which property changes the background color?

The background-color property sets the background color of an element. The background of an element is the total size of the element, including padding and border (but not the margin). Tip: Use a background color and a text color that makes the text easy to read.


1 Answers

The cause of it is that your css rules from bootstrap is being applied to tds, while your .hovered is being applied to tr.

Basically, when you click on it, you have grey table cells (td) over a black row (tr). You also need more weight on your selector, since .hovered is too weak and it's going to be overwritten by other rules (avoid using !important, or you could get on an infinite !important css war!):

.table-hover tbody tr.hovered td {
    background-color: #000000;
    color: #ffffff;
}

Also, you don't necessarily need to add > selectors to make the hover effect, unless you have other tables inside that one. Another thing is that your th are inside a thead, not tbody:

.table-hover tbody tr:hover td, .table-hover thead tr:hover th {
    background-color: #550055;
    color: #eeeeee;
}

Here's a fixed fork for your code:

http://www.bootply.com/mwFvWpMiGa

like image 94
v42 Avatar answered Sep 27 '22 17:09

v42