Here the following operations will be done:
* Styling even rows
* Hovering effect when placing the mouse over a row
* Change the color of a row when clicking on it or on its related checkbox.
* Implementing a "check all" checkbox.
It has only one problem:
* If you first click the "check all" checkbox, the other checkboxes will be checked, but the color of the rows won't change. Unfortunately the "check all" checkbox will only work if you click on a row or on another checkbox
$(function () {
//Add the style: 'even_row' for even rows
$('table tbody tr:even').addClass('even_row');
//On mouse hover, add the style: 'highlight_row'. When the mouse
//goes out, remove it
$("#check_all").change(function () {
//First the checkbox statuses will be changed according to
//the status of the master checkbox
$('input:checkbox').prop('checked', this.checked);
//Then the change event defined below will be triggered. By
//doing this, the row color will be changed according to the
//checkbox status
$('tr').toggleClass("selected_row", this.checked)
});
//This changes the color of a row and the checkbox status
//whenever the user clicks on the row or the checkbox.
//In order to exclude the header row, it will be applied only to the
//tbody tr selector. The 'check_all' check box must be excluded as well
//in order to not change the color of the header row.
$('table tbody :checkbox').change(function (event) {
$(this).closest('tr').toggleClass("selected_row", this.checked);
});
$('table tbody tr').click(function(event) {
if (event.target.type !== 'checkbox') {
$(':checkbox', this).trigger('click');
}
$("input[type='checkbox']").not('#check_all').change(function(e) {
if($(this).is(":checked")){
$(this).closest('tr').addClass("selected_row");
}else{
$(this).closest('tr').removeClass("selected_row");
}
});
});
});
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.