JQuery class not being removed on data table on row click - html

I have created a JS Fiddle for the issue I am having using JQuery with datatables: https://jsfiddle.net/f3xme87n/
I have a checkbox column which allows users to select maximum 5 items at once. The user can then select a primary row which can only ever be one row at a time (highlighted in yellow). Currently when you uncheck a selected box it highlights the row to yellow which is not what I want it to do.
To replicate the bug:
Hold Cntrl and click 5 checkbox items (the maximum you can select) these should highlight in a light blue colour.
Now Click on a name of one of selected, the row goes yellow which is expected
Now click on another name the row goes yellow and the previous row goes back to normal colour
Now uncheck one of the boxes from what you have selected (not the current yellow one) - Keep hold of Cntrl
Bug: the row remains yellow but unchecked. I need this to go back to normal table row colour so shouldn't have the primary class applied to that row.
I can seem to figure out how to remove the primary class when you uncheck the box! Hopefully JS Fiddle and above replication details help with my question.
Issue lies in this part of the code in the JS:
if (this.classList.contains('selected')) {
var prevSelectedItem = document.querySelector('tr.primary');
if (prevSelectedItem != null) {
prevSelectedItem.classList.remove('primary');
}
this.classList.add("primary");
}
I need to ensure in the Javascript - The user can only set the colour of a row to yellow if it has already been selected (i.e. blue) Otherwise rows should not be able to go to yellow at all.
Additional info:
The way the checkbox is checked is via css ::before and ::after. How can I retrieve the CSS to know whether the before and after is applied in my as that is how I can determine whether the checkbox is ticked or not:
table.dataTable tbody td.select-checkbox:before, table.dataTable tbody th.select-checkbox:before {
content: '';
margin-top: -6px;
margin-left: -6px;
border: 1px solid black;
border-radius: 3px;
}
table.dataTable tr.selected td.select-checkbox:after, table.dataTable tr.selected th.select-checkbox:after {
content: '\2714';
margin-top: -11px;
margin-left: -4px;
text-align: center;
text-shadow: 1px 1px #B0BED9, -1px -1px #B0BED9, 1px -1px #B0BED9, -1px 1px #B0BED9;
}

You can check if the tr clicked has class selected depending on this add class or remove same from tr.
Demo Code :
$(document).ready(function() {
var table = $('#tabledt').DataTable({
columnDefs: [{
orderable: false,
className: 'select-checkbox',
targets: [0]
}],
select: {
style: 'os',
selector: 'td:first-child'
},
order: [
[1, 'asc']
],
bSortClasses: false
});
table.on('select', function(e, dt, type, ix) {
var selected = dt.rows({
selected: true
});
if (selected.count() > 5) {
dt.rows(ix).deselect();
}
//remove class...
$("tbody tr:not(.selected)").removeClass("primary")
});
//on click of tr
$(document).on("click", "#tabledt tr", function() {
if ($(this).hasClass("selected")) {
//check if slectd class length is > 1
if ($("tbody").find(".selected").length > 1) {
$(this).removeClass("primary") //remove that primary class
$("tbody tr.selected").removeClass("primary")
} else {
//add class
$(this).addClass("primary") //add primary
}
} else {
$(this).removeClass("primary") //remove that primary class
}
//just other way to remove class
$("tbody tr:not(.selected)").removeClass("primary")
})
});
table.dataTable th.selectall-checkbox,
table.dataTable td.selectall-checkbox {
cursor: pointer;
outline: none;
text-align: center;
}
.primary {
background-color: yellow !important;
}
.selected {
background-color: #acbad4;
}
<link rel="stylesheet" href="https://cdn.datatables.net/select/1.1.2/css/select.dataTables.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.11/css/jquery.dataTables.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/responsive/2.0.2/css/responsive.dataTables.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.datatables.net/1.10.11/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/select/1.1.2/js/dataTables.select.min.js"></script>
<script src="https://cdn.datatables.net/responsive/2.0.2/js/dataTables.responsive.min.js"></script>
<style>
table.dataTable th.selectall-checkbox,
table.dataTable td.selectall-checkbox {
cursor: pointer;
outline: none;
text-align: center;
}
.primary {
background-color: yellow !important;
}
.selected {
background-color: #acbad4;
}
</style>
<table id="tabledt" class="display" style="width:100%">
<thead>
<tr>
<th></th>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Salary</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td>Tiger Nixon</td>
<td>System Architect</td>
<td>Edinburgh</td>
<td>61</td>
<td>$320,800</td>
</tr>
<tr>
<td></td>
<td>Garrett Winters</td>
<td>Accountant</td>
<td>Tokyo</td>
<td>63</td>
<td>$170,750</td>
</tr>
<tr>
<td></td>
<td>Ashton Cox</td>
<td>Junior Technical Author</td>
<td>San Francisco</td>
<td>66</td>
<td>$86,000</td>
</tr>
<tr>
<td></td>
<td>Cedric Kelly</td>
<td>Senior Javascript Developer</td>
<td>Edinburgh</td>
<td>22</td>
<td>$433,060</td>
</tr>
<tr>
<td></td>
<td>Airi Satou</td>
<td>Accountant</td>
<td>Tokyo</td>
<td>33</td>
<td>$162,700</td>
</tr>
<tr>
<td></td>
<td>Brielle Williamson</td>
<td>Integration Specialist</td>
<td>New York</td>
<td>61</td>
<td>$372,000</td>
</tr>
<tr>
<td></td>
<td>Herrod Chandler</td>
<td>Sales Assistant</td>
<td>San Francisco</td>
<td>59</td>
<td>$137,500</td>
</tr>
<tr>
<td></td>
<td>Rhona Davidson</td>
<td>Integration Specialist</td>
<td>Tokyo</td>
<td>55</td>
<td>$327,900</td>
</tr>
<tr>
<td></td>
<td>Gloria Little</td>
<td>Systems Administrator</td>
<td>New York</td>
<td>59</td>
<td>$237,500</td>
</tr>
<tr>
<td></td>
<td>Bradley Greer</td>
<td>Software Engineer</td>
<td>London</td>
<td>41</td>
<td>$132,000</td>
</tr>
</tbody>
<tfoot>
<tr>
<th></th>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Salary</th>
</tr>
</tfoot>
</table>

table.on("deselect", function (e, dt, type, ix) {
dt.rows(ix).nodes().to$().removeClass("primary");
});
if (
!e.target.className.includes("select-checkbox") &&
this.classList.contains("selected")
) {
var prevSelectedItem = document.querySelector("tr.primary");
if (prevSelectedItem != null) {
prevSelectedItem.classList.remove("primary");
}
this.classList.add("primary");
}

You just need to inhibit click handler when it comes from that checkbox:
table.rows[i].onclick = function ({originalTarget:ot}) {
if($(ot).hasCElass("select-checkbox")) return;
/* ... */
};
Edit:
Answering your comment: If I understood your intention well, that jsfiddle works to me.
Maybe you're trying from an old browser not supporting destructuring.
Try this instead:
table.rows[i].onclick = function (ev) {
if($(ev.originalTarget).hasCElass("select-checkbox")) return;
/* ... */
};
orininalTarget is a property of passed-in event object that points to the actual DOM element that received the click (even if, like in your case, you were listening on an outer one that contains it).

Related

How to remove HTML table row by value

Let's say there is a table:
<table id="tblPotrawySkladniki">
<tbody>
<tr><td>banana</td><td>10</td></tr>
<tr><td>orange</td><td>20</td></tr>
<tr><td>raspberry</td><td>20</td></tr>
</tbody>
</table>
I would like to remove entire row where i.e. cell1 = orange.
How can I do this using jquery?
Consider the following two examples. Example 1:
$(function() {
$("#tblPotrawySkladniki > tbody td").each(function(i, el) {
if ($(el).text() === "orange") {
$(el).parent().remove();
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="tblPotrawySkladniki">
<tbody>
<tr>
<td>banana</td>
<td>10</td>
</tr>
<tr>
<td>orange</td>
<td>20</td>
</tr>
<tr>
<td>raspberry</td>
<td>20</td>
</tr>
</tbody>
</table>
This first example gives you more control over how you compare or seek out the cell. For example, you could use:
$(el).text().trim().toLowerCase() === "orange"
This would help ensure a case insensitive search.
Example 2:
$(function() {
$("#tblPotrawySkladniki > tbody td:contains('orange')").parent().remove();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="tblPotrawySkladniki">
<tbody>
<tr>
<td>banana</td>
<td>10</td>
</tr>
<tr>
<td>orange</td>
<td>20</td>
</tr>
<tr>
<td>raspberry</td>
<td>20</td>
</tr>
</tbody>
</table>
The second example relies on the selector and if they do not match exactly, will not find it. It's quick and uses less lines, yet may not always find the needle.
Each of these, in their own way, will target the Cell and remove the parent Row. See More:
https://api.jquery.com/contains-selector/
https://api.jquery.com/text/
https://api.jquery.com/each/
Here is a dynamic way of doing it. For example, enter Orange or Raspberry into the input and click enter.
$(function() {
$("#inputSearch").on('change', function(){
var v = $(this).val();
$('#tblPotrawySkladniki tr td').filter(function() {
if ($(this).text() === v) {
return true;
}
}).parent().remove();
});
})
table {
margin: 0 auto;
}
tr {
border: 1px solid black;
}
tbody {
border: 1px solid black;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label for="inputSearch">Search Value:</label>
<input type="text" name="inputSearch" id="inputSearch">
<table id="tblPotrawySkladniki">
<tbody>
<tr><td>banana</td><td>10</td></tr>
<tr><td>orange</td><td>20</td></tr>
<tr><td>raspberry</td><td>20</td></tr>
</tbody>
</table>

How to hover <td> that contain specific data

I need to display red background color in <td> that I hovered. For example, if I hovered 'Apple', then 'Apple' in all <td> shall be hovered same color as well. Currently can only hover one <td>Apple</td>.
table {
margin: 2rem;
}
th, td {
border: 1px solid #333;
}
td:hover{
background-color:red
}
html {
font-size: 24px;
}
<h3>Table 1</h3>
<table>
<tr>
<th>Header 1.1</th>
<th>Header 1.2</th>
<th>Header 1.3</th>
</tr>
<tr>
<td>Apple</td>
<td>Orange</td>
<td>Lemon</td>
</tr>
<tr>
<td>Orange</td>
<td>Lemon</td>
<td>Apple</td>
</tr>
</table>
Codepen
You can do that with the help of jQuery. Try running the following snippet.
$('.apple').hover(
function(){
$('.apple').css({"background":"red"});
},function(){
$('.apple').css({"background":"white"});
})
$('.orange').hover(
function(){
$('.orange').css({"background":"orange"});
}
,function(){
$('.orange').css({"background":"white"});
}
)
$('.lemon').hover(
function(){
$('.lemon').css({"background":"yellow"});
}, function(){
$('.lemon').css({"background":"white"});
})
html {
font-size: 24px;
}
table {
margin: 2rem;
}
th, td {
border: 1px solid #333;
}
td span {
display: block;
}
td:hover span.apple {
background-color:red
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h3>Table 1</h3>
<table>
<tr>
<th>Header 1.1</th>
<th>Header 1.2</th>
<th>Header 1.3</th>
</tr>
<tr>
<td><span class="apple">Apple</span></td>
<td><span class="orange">Orange</span></td>
<td><span class="lemon">Lemon</span></td>
</tr>
<tr>
<td><span class="orange">Orange</span></td>
<td><span class="lemon">Lemon</span></td>
<td><span class="apple">Apple</span></td>
</tr>
</table>
This cannot be done with just HTML and CSS as CSS is not aware of content.
Using Javascript you can set CSS variables that in turn will set the background of a cell.
This snippet goes through each td element and sets the style background: var(--name of fruit) so for example all apple cells have the style="background: var(--apple);" added to them. Then when a td is hovered the JS sets the --apple to red and when the mouse moves out it sets it to transparent.
That way all those tds with background: var(--apple) get highlighted.
There is no need to iterate through all the cells in the table each time a hover takes place, you can do it by setting everything up once at the start.
function setHighlight(e) {
table.style.setProperty('--' + e.target.textContent, 'red');
}
function removeHighlight(e) {
table.style.setProperty('--' + e.target.textContent, 'transparent');
}
const table = document.querySelector('table');
const tds = document.querySelectorAll('td');
tds.forEach(td => {
td.addEventListener('mouseover', setHighlight);
td.style.backgroundColor = 'var(--' + td.textContent + ')';
});
tds.forEach(td => {
td.addEventListener('mouseout', removeHighlight);
});
<h3>Table 1</h3>
<table>
<tr>
<th>Header 1.1</th>
<th>Header 1.2</th>
<th>Header 1.3</th>
</tr>
<tr>
<td>Apple</td>
<td>Orange</td>
<td>Lemon</td>
</tr>
<tr>
<td>Orange</td>
<td>Lemon</td>
<td>Apple</td>
</tr>
</table>
Add a class in every td and use JQuery.
See the example below.
$(document).ready(function(){
$("td.apple").hover(function(){
$(".apple").css("background-color", "red");
}, function(){
$(".apple").css("background-color", "white");
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr>
<th>Company</th>
<th>Contact</th>
<th>Country</th>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td class="apple">Apple</td>
<td class="apple">Apple</td>
</tr>
<tr>
<td>Centro comercial Moctezuma</td>
<td class="apple">Apple</td>
<td>Mexico</td>
</tr>
<tr>
<td>Ernst Handel</td>
<td>Roland Mendel</td>
<td class="apple">Apple</td>
</tr>
<tr>
<td>Island Trading</td>
<td>Helen Bennett</td>
<td>UK</td>
</tr>
<tr>
<td>Laughing Bacchus Winecellars</td>
<td>Yoshi Tannamuri</td>
<td>Canada</td>
</tr>
<tr>
<td>Magazzini Alimentari Riuniti</td>
<td>Giovanni Rovelli</td>
<td>Italy</td>
</tr>
</table>
If You don't want to add extra IDs and add jquery as dependency to Your code, You can use vanilla JS
// Get all TDs
const tds = Array.from(document.querySelectorAll("td"));
tds.map(td => {
// bind mouseenter to TDs to paint BG
td.addEventListener("mouseenter", (event) => {
const text = event.target.textContent;
// paint TDs with same text
tds.map(td => {
if(td.textContent === text) {
td.style.background = 'red';
}
});
});
// bind mouseleave to TDs to remove BG
td.addEventListener("mouseleave", (event) => {
tds.map(td => {
td.style.background = 'transparent';
});
})
});
Working example: https://codepen.io/ipasha/pen/eYRKxpP
table {
margin: 2rem;
}
th, td {
border: 1px solid #333;
}
.apple:hover{
background-color:red
}
html {
font-size: 24px;
}
<h3>Table 1</h3>
<table>
<tr>
<th>Header 1.1</th>
<th>Header 1.2</th>
<th>Header 1.3</th>
</tr>
<tr>
<td class="apple">Apple</td>
<td>Orange</td>
<td>Lemon</td>
</tr>
<tr>
<td>Orange</td>
<td>Lemon</td>
<td class="apple">Apple</td>
</tr>
</table>
This is one way you can try:
html {
font-size: 24px;
}
table {
margin: 2rem;
}
th, td {
border: 1px solid #333;
}
td span {
display: block;
}
td:hover span.apple {
background-color:red
}
<h3>Table 1</h3>
<table>
<tr>
<th>Header 1.1</th>
<th>Header 1.2</th>
<th>Header 1.3</th>
</tr>
<tr>
<td><span class="apple">Apple</span></td>
<td><span>Orange</span></td>
<td><span>Lemon</span></td>
</tr>
<tr>
<td><span>Orange</span></td>
<td><span>Lemon</span></td>
<td><span class="apple">Apple</span></td>
</tr>
</table>

Counting values on click in table

I'm trying to make a table that when you click on it the clicked row turns green and the value that is in the table gets counted, right now it just counts the amount of rows clicked but I want to change this to the amount that is under the value.
$(function() {
var countEl = $("#count");
var countE2 = $("#Value")
var count = 0;
$('tbody tr').click(function() {
$(this).toggleClass("green-cell");
if ($(this).hasClass("green-cell")) {
count++;
} else {
count--;
}
countEl.html(count);
});
});
.green-cell {
background: rgb(29, 247, 0);
}
table {
border-collapse: collapse;
}
td,
th {
border: solid 1px #cccccc;
}
td,
th {
padding: 5px;
}
tbody tr {
cursor: pointer;
}
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
Count: <span id="count"> 0</span>
<br/><br/>
<table class="table " id="onclick">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Count</th>
</tr>
</thead>
<tbody>
<tr>
<td>John</td>
<td>Doe</td>
<td>1</td>
</tr>
<tr>
<td>Mary</td>
<td>Moe</td>
<td>2</td>
</tr>
<tr>
<td>July</td>
<td>Dooley</td>
<td>3</td>
</tr>
<tr>
<td>henk</td>
<td>janssen</td>
<td>4</td>
</tr>
<tr>
<td>piet</td>
<td>Paulisma</td>
<td>5</td>
</tr>
<tr>
<td>Theo</td>
<td>van gogh</td>
<td>6</td>
</tr>
<tr>
<td>Erik</td>
<td>Doerustig</td>
<td>7</td>
</tr>
<tr>
<td>Jan</td>
<td>de steen</td>
<td>8</td>
</tr>
</tbody>
</table>
</div>
</body>
Get the row's last TD element by using .eq( -1 )
Get it's .text() and use parseInt to convert string to integer:
$(function() {
var countEl = $("#count");
var countE2 = $("#Value")
var count = 0;
$('tbody tr').click(function() {
var $td = $(this).find("td").eq( -1 ); // Get desired row's cell
var tdVal = parseInt( $td.text(), 10); // Convert content string to integer
$(this).toggleClass("green-cell");
if ($(this).hasClass("green-cell")) {
count+= tdVal;
} else {
count-= tdVal;
}
countEl.html(count);
});
});
.green-cell {
background: rgb(29, 247, 0);
}
table {
border-collapse: collapse;
}
td,
th {
border: solid 1px #cccccc;
}
td,
th {
padding: 5px;
}
tbody tr {
cursor: pointer;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="container">
Count: <span id="count"> 0</span>
<br/><br/>
<table class="table " id="onclick">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Count</th>
</tr>
</thead>
<tbody>
<tr>
<td>John</td>
<td>Doe</td>
<td>1</td>
</tr>
<tr>
<td>Mary</td>
<td>Moe</td>
<td>2</td>
</tr>
<tr>
<td>July</td>
<td>Dooley</td>
<td>3</td>
</tr>
<tr>
<td>henk</td>
<td>janssen</td>
<td>4</td>
</tr>
<tr>
<td>piet</td>
<td>Paulisma</td>
<td>5</td>
</tr>
<tr>
<td>Theo</td>
<td>van gogh</td>
<td>6</td>
</tr>
<tr>
<td>Erik</td>
<td>Doerustig</td>
<td>7</td>
</tr>
<tr>
<td>Jan</td>
<td>de steen</td>
<td>8</td>
</tr>
</tbody>
</table>
If I understand what your trying to do correctly, which is add the value found in the count column when you click a row you could do something like this:
var countEl = $("#count");
var countE2 = $("#Value")
var count = 0;
$('tbody tr').click(function() {
$(this).toggleClass("green-cell");
if ($(this).hasClass("green-cell")) {
count += parseInt($(this).find('td:last-of-type()').text());
} else {
count -= parseInt($(this).find('td:last-of-type()').text());
}
countEl.html(count);
});
Just change this:
$('tbody tr').click(function() {
$(this).toggleClass("green-cell");
if ($(this).hasClass("green-cell")) {
count++;
} else {
count--;
}
countEl.html(count);
});
To this:
$('tbody tr').click(function() {
$(this).toggleClass("green-cell");
var value = parseInt($(this).children("td:last").html());
if ($(this).hasClass("green-cell")) {
count +=value;
} else {
count -=value;
}
countEl.html(count);
});
We grab the last TD, the one with the value, and convert that to INT with parseInt
If I understand you correct you want to have the sum of all values of column "Count".
By now you're just increasing your local counter. Instead you need to get the value of the column Count and add it to your current value.
So basically you need to change your Javascript like this
$(function() {
var countEl = $("#count");
var countE2 = $("#Value")
var count = 0;
$('tbody tr').click(function() {
$(this).toggleClass("green-cell");
var curCount = parseInt($(this).find('td:last').text());
if ($(this).hasClass("green-cell")) {
count = count + curCount;
} else {
count = count - curCount;
}
countEl.html(count);
});
});
My jQuery is pretty rusty, but what you in essence want to do here is find your last table cell in the clicked row and add/subtract that instead of just incrementing/decrementing your counter.
let amount = Number.parseInt($el.children('td').last().text());
This line is saying that the amount is equal to an integer found at the last td of the clicked row. Then you would add it to your count, the only other complication is the need to subtract if the cell isn't active, it is generally good practice to avoid else conditions (helps remove catch all unexplained effects) so I edited the code to add in all cases and to make the number negative in the case of an inactive cell.
$(function() {
const $countEl = $('#count');
let count = 0;
$('tbody').on('click', 'tr', function() {
const $el = $(this);
let amount = Number.parseInt($el.children('td').last().text());
$el.toggleClass('green-cell')
if (!$el.hasClass('green-cell')) {
amount = -amount;
}
count += amount;
$countEl.text(count);
});
});
.green-cell {
background: rgb(29, 247, 0);
}
table {
border-collapse: collapse;
}
td,
th {
border: solid 1px #cccccc;
}
td,
th {
padding: 5px;
}
tbody tr {
cursor: pointer;
}
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
Count: <span id="count"> 0</span>
<br/><br/>
<table class="table " id="onclick">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Count</th>
</tr>
</thead>
<tbody>
<tr>
<td>John</td>
<td>Doe</td>
<td>1</td>
</tr>
<tr>
<td>Mary</td>
<td>Moe</td>
<td>2</td>
</tr>
<tr>
<td>July</td>
<td>Dooley</td>
<td>3</td>
</tr>
<tr>
<td>henk</td>
<td>janssen</td>
<td>4</td>
</tr>
<tr>
<td>piet</td>
<td>Paulisma</td>
<td>5</td>
</tr>
<tr>
<td>Theo</td>
<td>van gogh</td>
<td>6</td>
</tr>
<tr>
<td>Erik</td>
<td>Doerustig</td>
<td>7</td>
</tr>
<tr>
<td>Jan</td>
<td>de steen</td>
<td>8</td>
</tr>
</tbody>
</table>
</div>
</body>
You can grab the amount from the last cell from the row by using cells property of the <tr> element.
const amount = +this.cells[this.cells.length - 1].innerText;
if ($(this).hasClass("green-cell")) {
count += amount
} else {
count -= amount
}
Then, instead of incrementing or decrementing, use this value to add/subtract.
$(function() {
var countEl = $("#count");
var countE2 = $("#Value")
var count = 0;
$('tbody tr').click(function() {
$(this).toggleClass("green-cell");
const amount = +this.cells[this.cells.length - 1].innerText;
if ($(this).hasClass("green-cell")) {
count += amount
} else {
count -= amount
}
countEl.html(count);
});
});
.green-cell {
background: rgb(29, 247, 0);
}
table {
border-collapse: collapse;
}
td, th {
border: solid 1px #cccccc;
}
td, th {
padding: 5px;
}
tbody tr {
cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Count: <span id="count"> 0</span>
<table class="table " id="onclick">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Count</th>
</tr>
</thead>
<tbody>
<tr>
<td>John</td>
<td>Doe</td>
<td>1</td>
</tr>
<tr>
<td>Mary</td>
<td>Moe</td>
<td>2</td>
</tr>
<tr>
<td>July</td>
<td>Dooley</td>
<td>3</td>
</tr>
<tr>
<td>henk</td>
<td>janssen</td>
<td>4</td>
</tr>
<tr>
<td>piet</td>
<td>Paulisma</td>
<td>5</td>
</tr>
<tr>
<td>Theo</td>
<td>van gogh</td>
<td>6</td>
</tr>
<tr>
<td>Erik</td>
<td>Doerustig</td>
<td>7</td>
</tr>
<tr>
<td>Jan</td>
<td>de steen</td>
<td>8</td>
</tr>
</tbody>
</table>

how to find empty html td cell and change back ground color through php html css

how to find empty/error value html cell and change back ground color through php html css
Here status column don't have a value, I want to change the body class background color to red if any of the html table cell value is empty and color to green if all values are present and color to yellow if status has value "E" . Could you please help me on this.
<link rel="stylesheet" type="text/css" href="/Export/rbody.css">
<BODY class="red">
<tr>
<th>database()</th>
<th>id</th>
<th>PROCESS</th>
<th>SCHEDULENEXTDUE</th>
<th>STATUS</th>
<th>LASTUPDATEDON</th>
</tr>
<tr>
<td>CUST_TEST_DB</td>
<td>5</td>
<td>restart</td>
<td>2018-02-02 07:35:52</td>
<td></td>
<td>2018-02-02 07:35:52</td>
</tr>
body {
color: #D8D8BF;
background-color: black;
background-repeat: repeat-y;
}
a:link { color: #00FFAA; text-decoration: underline; }
a:visited { color: #FFFF44; text-decoration: underline; }
.green {
background-image: url(green.gif);
}
.red {
background-image: url(red.gif);
}
.yellow {
background-image: url(yellow.gif);
}
You could use the :empty pseudo selector in CSS.
td:empty {
background-color: red;
}
<table>
<tr>
<th>database()</th>
<th>id</th>
<th>PROCESS</th>
<th>SCHEDULENEXTDUE</th>
<th>STATUS</th>
<th>LASTUPDATEDON</th>
</tr>
<tr>
<td>CUST_TEST_DB</td>
<td>5</td>
<td>restart</td>
<td>2018-02-02 07:35:52</td>
<td></td>
<td>2018-02-02 07:35:52</td>
</tr>
</table>
You can try using the css empty selector, like this perhaps:
td:empty{
background:red;
}
After reading the comments and then reading the question in more detail I appreciate that the original answer I gave, whilst definitely succinct, was perhaps not sufficient in all respects. The following quickly written piece of javascript does some basic tests and assigns a rudimentary class accordingly.
<table>
<tr>
<th>database()</th>
<th>id</th>
<th>PROCESS</th>
<th>SCHEDULENEXTDUE</th>
<th>STATUS</th>
<th>LASTUPDATEDON</th>
</tr>
<tr>
<td>CUST_TEST_DB</td>
<td>5</td>
<td>restart</td>
<td>2018-02-02 07:35:52</td>
<td>E</td>
<td>2018-02-02 07:35:52</td>
</tr>
<tr>
<td>CUST_TEST_DB</td>
<td>5</td>
<td>restart</td>
<td></td>
<td>2018-02-02 07:35:52</td>
<td>2018-02-02 07:35:52</td>
</tr>
<tr>
<td>CUST_TEST_DB</td>
<td>5</td>
<td></td>
<td>restart</td>
<td>2018-02-02 07:35:52</td>
<td>2018-02-02 07:35:52</td>
</tr>
</table>
<style>
td:empty{
background:red;
display:block;
}
.empty{
background:red;
}
.notempty{
background:green;
}
.status_E{
background:yellow;
}
</style>
<script>
Array.prototype.slice.call( document.querySelectorAll('td') ).forEach( function( td ){
if( td.innerHTML!=''){
td.classList.add('notempty');
}
if( td.innerHTML=='E'){
td.classList.add('status_E');
}
if( td.innerHTML=='' ){
td.classList.add('empty');
td.innerHTML=' '
}
});
</script>
or, assuming the data is drawn from a database initially then some pseudo-code to indicate another possible approach ( not tested or debugged )
echo "
<table>
<tr>
<th>database()</th>
<th>id</th>
<th>PROCESS</th>
<th>SCHEDULENEXTDUE</th>
<th>STATUS</th>
<th>LASTUPDATEDON</th>
</tr>";
/* assumed names of columns */
$fields=array('db','id','process','nextdue','status','updated');
$class='';
while( $row=$results->fetch() ){
foreach( $fields as $field ){
if( empty( $row->$field ) ) $class=" class='empty'";
if( $field=='status' && $row->$field=='E' ) $class=" class='status_E'";
echo "<td{$class}>{$row->$field}</td>";
}
}
echo "</table>";

Blogger Active LINK Class

Here is my html menu button :
<tr>
<td>23</td>
<td>24</td>
<td>25</td>
</tr>
How to add this class on current URL?
<td class="active">25</td>
How can I accomplish to make this work?
$('table td a').click(function(e){
$('table td').removeClass('active');
$(this).parent('td').addClass('active');
e.preventDefault();
});
.active a{
color: #f00;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<table>
<tr>
<td>23</td>
<td>24</td>
<td>25</td>
</tr>
</table>
Here is the solution. Use it according to your needs.