How to switch table row background color for twitter bootstrap?

Regarding my previous post :

My html

<table class="table table-bordered table-condensed table-striped"> <thead> <tr> <th>Col 1</th> <th>Col 2</th> <th>Col 3</th> </tr> </thead> <tbody> <tr> <td><strong>Data 1</strong></td> <td>Data 2</td> <td>Data 3</td> </tr> <tr> <td><strong>Data 1</strong></td> <td>Data 2</td> <td>Data 3</td> </tr> <tr> <td><strong>Data 1</strong></td> <td>Data 2</td> <td>Data 3</td> </tr> </tbody> </table> 

JQuery

 $(document).ready(function(){ $('table.table-striped tr').on('click', function () { $(this).find('td').css('background-color', '#ff0000'); // toggleClass doesn't seem to work }); }); 

I am trying to toggle the color of a string using through the click event. Any idea if this is possible with a custom css selector?

+4
source share
1 answer

You can try this

CSS

 .bg{background-color:#ff0000 !important;}​ 

Js

 $(document).ready(function(){ $('table.table-striped tbody tr').on('click', function () { $(this).closest('table').find('td').removeClass('bg'); $(this).find('td').addClass('bg'); }); });​ 

Demo .

Update: To switch

 $(document).ready(function(){ $('table.table-striped tbody tr').on('click', function () { $(this).find('td').toggleClass('bg'); }); });​ 

Demo .

+5
source

Source: https://habr.com/ru/post/1436663/


All Articles