JQuery select & reset (clear) date range in HTML Calendar - html

I use a HTML calendar in which you can select a date range. Unfortunately it is not possible to delete a selected range. My idea would be to solve this with even and odd:
First click (odd) select first day
Second click (even) select range
Third click (odd) delete all selects and select new first day
Fourth click (even) select new range
...
I am glad about tips and help. I can't get any further on my own.
$(document).ready(function() {
$('td.day').click(function() {
if ($("td.firstClick").length == 0) {
$(this).addClass("firstClick");
}
});
$('td.day').hover(function() {
if ($("td.firstClick").length == 0) {
$(this).addClass("selected");
return;
}
$(this).addClass("secondClick");
var tds = $('td.day');
var firstClick = $(".firstClick");
var firstClickIndex = tds.index(firstClick);
var currentIndex = tds.index(this);
tds.filter(function() {
var idx = tds.index(this);
return idx >= firstClickIndex && idx <= currentIndex;
}).addClass("selected")
}, function() {
if ($("td.firstClick").length == 0) {
$(this).removeClass("selected");
return;
}
$(this).removeClass("secondClick");
var tds = $('td.day');
var firstClick = $(".firstClick");
var firstClickIndex = tds.index(firstClick);
var currentIndex = tds.index(this);
tds.filter(function() {
var idx = tds.index(this);
return idx >= firstClickIndex && idx <= currentIndex;
}).removeClass("selected")
});
$('table').on('click', 'td.secondClick', function() {
$('.selected').addClass('reserved');
});
});
table {
border-collapse: collapse;
}
table tr td {
width: 14%;
}
table tr td:hover {
cursor: pointer;
}
.firstClick {
background: green;
}
.selected {
background: lightgreen;
}
.reserved {
background: red !important;
}
.secondClick {
background: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table border="1">
<tr>
<td colspan="7"><b>2016</b>
</td>
</tr>
<tr>
<td colspan="7"><i>November</i>
</td>
</tr>
<tr>
<th>mon</th>
<th>tue</th>
<th>wed</th>
<th>thu</th>
<th>fri</th>
<th>sat</th>
<th>sun</th>
</tr>
<tr>
<td></td>
<td class="day">1</td>
<td class="day">2</td>
<td class="day">3</td>
<td class="day">4</td>
<td class="day">5</td>
<td class="day">6</td>
</tr>
<tr>
<td class="day">7</td>
<td class="day">8</td>
<td class="day">9</td>
<td class="day">10</td>
<td class="day">11</td>
<td class="day">12</td>
<td class="day">13</td>
</tr>
<tr>
<td class="day">14</td>
<td class="day">15</td>
<td class="day">16</td>
<td class="day">17</td>
<td class="day">18</td>
<td class="day">19</td>
<td class="day">20</td>
</tr>
<tr>
<td class="day">21</td>
<td class="day">22</td>
<td class="day">23</td>
<td class="day">24</td>
<td class="day">25</td>
<td class="day">26</td>
<td class="day">27</td>
</tr>
<tr>
<td class="day">28</td>
<td class="day">29</td>
<td class="day">30</td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>

remove class reserved, selected and secondClick in td.day click function to remove range selected before
$('td.day').click(function() {
if ($("td.firstClick").length == 0) {
$('td.day').removeClass("reserved");
$('td.day').removeClass("selected");
$('td.day').removeClass("secondClick");
$(this).addClass("firstClick");
}
});
and in table td.secondClick click function remove class firstClick to reinit new firstClick
$('table').on('click', 'td.secondClick', function() {
$('.selected').addClass('reserved');
$('td.day').removeClass("firstClick");
});

Related

jquery func not working to highlight 3 continuous max values of the input value from 2 html table cells

When the user enters the table cell value say for example 65000 in the input textbox named BASIC, matching figure will be highlighted with red and the very next value i.e. 67000 will be highlighted to blue.
How to extend the search to the next table? The next closest value in the other table cell is 67700. The result i.e. 67700 could be shown in the Next Basic textbox.
$(function() {
$('#cb').on('change keyup', function() {
var search = $(this).val();
$('#le10 tr td, #le11 tr td').filter(function() {
if ($(this).text() == search) {
$(this).parent('tr').addClass('highlight');
$(this).parent('tr').closest('tr').next().addClass('highlight2');
$(this).parent('#le10 tr').closest('#le11 tr').next().addClass('highlight3');
} else {
$(this).parent('tr').removeClass('highlight');
$(this).parent('tr').closest('tr').next().removeClass('highlight2');
$(this).parent('#le10 tr').closest('#le11 tr').next().removeClass('highlight3');
}
})
});
});
.highlight {
color: red;
background-color: yellow;
font-weight: bold;
}
.highlight2 {
color: blue;
background-color: greenyellow;
font-weight: bold;
}
.highlight3 {
color: green;
background-color: yellow;
font-weight: bold;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<div class="container">
<div class="row">
<div class="col-md-6">
<table width="100%" border="0">
<tr>
<td>Basic</td>
<td><input class="form-control" type="text" name="cb" id="cb" autocomplete="off" /></td>
</tr>
</table>
</div>
</div>
</div>
<table class="table table-responsive">
<tr>
<td>
<h6>Current Level</h6>
</td>
<td>
<h6>Next Level</h6>
</td>
<td>
<h6>Next Basic</h6>
</td>
</tr>
<tr>
<td>
<table id="le10" class="table table-responsive table-striped">
<tr>
<td>56100</td>
</tr>
<tr>
<td>57800</td>
</tr>
<tr>
<td>59500</td>
</tr>
<tr>
<td>61300</td>
</tr>
<tr>
<td>63100</td>
</tr>
<tr>
<td>65000</td>
</tr>
<tr>
<td>67000</td>
</tr>
</table>
</td>
<td>
<table id="le11" class="table table-responsive table-striped">
<tr>
<td>67700</td>
</tr>
<tr>
<td>69700</td>
</tr>
<tr>
<td>71800</td>
</tr>
<tr>
<td>74000</td>
</tr>
</table>
</td>
<td>
Next Basic<input class="form-control" type="text" name="nb" id="nb" />
</td>
</tr>
</table>
You can highlight elements when looping through the elements list $('#le10 tr td, #le11 tr td'). For example, below I used hightlightCount for checking whether matched element is found, and highlight successive cells accordingly.
var highlightCount = 0;
$('#le10 tr td, #le11 tr td').each(function () {
if ($(this).text() == search) {
...
highlightCount = 2;
} else if (highlightCount == 2) {
// next cell of matched element
...
highlightCount--;
} else if (highlightCount == 1) {
// next next cell of matched element
...
highlightCount--;
} else {
...
}
});
I also changed .filter() to .each() so that it is more readable in this case of looping through all elements.
Here is the snippet:
$(document).ready(function() {
$('#cb').on('change keyup', function() {
var search = $(this).val();
searchText(search);
});
});
function searchText(search) {
// set counter for highlight
var highlightCount = 0;
$('#le10 tr td, #le11 tr td').each(function() {
if ($(this).text() == search) {
// found matched element
// reset Next Basic value
// hightlight cell and set hightlight counter
$('#nb').val('');
$(this).parent('tr').addClass('highlight');
highlightCount = 2;
} else if (highlightCount == 2) {
// next cell of matched element
// hightlight cell and decrease hightlight counter
$(this).parent('tr').addClass('highlight2');
highlightCount--;
} else if (highlightCount == 1) {
// next next cell of matched element
// set Next Basic to this element value
// hightlight cell and decrease hightlight counter
$('#nb').val($(this).text());
$(this).parent('tr').addClass('highlight3');
highlightCount--;
} else {
// remove hightlight for other cells
$(this).parent('tr').removeClass('highlight highlight2 highlight3');
}
});
}
.highlight {
color: red;
background-color: yellow;
font-weight: bold;
}
.highlight2 {
color: blue;
background-color: greenyellow;
font-weight: bold;
}
.highlight3 {
color: green;
background-color: yellow;
font-weight: bold;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<div class="container">
<div class="row">
<div class="col-md-6">
<table width="100%" border="0">
<tr>
<td>Basic</td>
<td><input class="form-control" type="text" name="cb" id="cb" autocomplete="off" /></td>
</tr>
</table>
</div>
</div>
</div>
<table class="table table-responsive">
<tr>
<td>
<h6>Current Level</h6>
</td>
<td>
<h6>Next Level</h6>
</td>
<td>
<h6>Next Basic</h6>
</td>
</tr>
<tr>
<td>
<table id="le10" class="table table-responsive table-striped">
<tr>
<td>56100</td>
</tr>
<tr>
<td>57800</td>
</tr>
<tr>
<td>59500</td>
</tr>
<tr>
<td>61300</td>
</tr>
<tr>
<td>63100</td>
</tr>
<tr>
<td>65000</td>
</tr>
<tr>
<td>67000</td>
</tr>
</table>
</td>
<td>
<table id="le11" class="table table-responsive table-striped">
<tr>
<td>67700</td>
</tr>
<tr>
<td>69700</td>
</tr>
<tr>
<td>71800</td>
</tr>
<tr>
<td>74000</td>
</tr>
</table>
</td>
<td>
Next Basic<input class="form-control" type="text" name="nb" id="nb" />
</td>
</tr>
</table>

How to style all elements that share data attribute values?

I have a generic table on my hands to which I've added a data attribute index with a value:
<table>
<tr> <td data-index="1"></td> <td data-index="2"></td> <td data-index="3"></td> <td data-index="4"></td></tr>
<tr> <td data-index="1"></td> <td data-index="2"></td> <td data-index="3"></td> <td data-index="4"></td></tr>
<tr> <td data-index="1"></td> <td data-index="2"></td> <td data-index="3"></td> <td data-index="4"></td></tr>
<tr> <td data-index="1"></td> <td data-index="2"></td> <td data-index="3"></td> <td data-index="4"></td></tr>
</table>
Would it be possible to influence all elements who share the same value of data-index through css?
Something like - CSS:
td[data-index.value]:hover{
background-color: red;
}
What I want to accomplish is that when I hover over one td element all the other td elements who share the data-index value (example all who are ="1") get styliside. I am trying to accomplish a vertical column highlight.
Sollution that relies on javascript (typescript) and react:
The way I solved it with react and typescript if someone finds this later:
const handleColumnHoverEnter = (e: any) => {
const allWithAttribute = Array.from(
document.querySelectorAll("[data-index='" + e.target.dataset.index + "']")
);
allWithAttribute.forEach((element:any) => {
element.style.backgroundColor = 'red';
});
}
const handleColumnHoverLeave = (e: any) => {
const allWithAttribute = Array.from(
document.querySelectorAll("[data-index='" + e.target.dataset.index + "']")
);
allWithAttribute.forEach((element:any) => {
element.style.backgroundColor = 'white';
});
}
Thanks to users bloodyKnucklese's and t.niese's suggestions!
Using Javascript to add mouseover and mouseout event handlers on each element. Get the element's dataset data-index value and use that to change the background on all elements having the same value:
document.querySelectorAll("td[data-index]").forEach(di => {
di.addEventListener("mouseover", (event) => {
document.querySelectorAll(`td[data-index="${di.dataset.index}"]`).forEach(
dii => { dii.style.backgroundColor = "#EEEEEE"; }
);
});
di.addEventListener("mouseout", (event) => {
document.querySelectorAll(`td[data-index="${di.dataset.index}"]`).forEach(
dii => { dii.style.backgroundColor = "#FFFFFF"; }
);
});
});
<table>
<tr> <td data-index="1">1</td> <td data-index="2">2</td> <td data-index="3">3</td> <td data-index="4">4</td></tr>
<tr> <td data-index="1">1</td> <td data-index="2">2</td> <td data-index="3">3</td> <td data-index="4">4</td></tr>
<tr> <td data-index="1">1</td> <td data-index="2">2</td> <td data-index="3">3</td> <td data-index="4">4</td></tr>
<tr> <td data-index="1">1</td> <td data-index="2">2</td> <td data-index="3">3</td> <td data-index="4">4</td></tr>
</table>
You could do something like that with the :has selector (which is at the time of writing only supported by Chrome 105+ and Safari 15.4+), so not really useful right now.
Anyhow, with :has you could have a slector like table:has([data-index="1"]:hover) [data-index="1"], which means, get the table elements that contains an element [data-index="1"] that is hovered, and select all descendance of that table that match [data-index="1"]
table:has([data-index="1"]:hover) [data-index="1"] {
background-color: red;
}
table:has([data-index="2"]:hover) [data-index="2"] {
background-color: red;
}
table:has([data-index="3"]:hover) [data-index="3"] {
background-color: red;
}
table:has([data-index="4"]:hover) [data-index="4"] {
background-color: red;
}
td {
width: 10px;
height: 10px;
border: 1px solid red;
}
td::before {
content: attr(data-index);
}
<table>
<tr> <td data-index="1"></td> <td data-index="2"></td> <td data-index="3"></td> <td data-index="4"></td></tr>
<tr> <td data-index="1"></td> <td data-index="2"></td> <td data-index="3"></td> <td data-index="4"></td></tr>
<tr> <td data-index="1"></td> <td data-index="2"></td> <td data-index="3"></td> <td data-index="4"></td></tr>
<tr> <td data-index="1"></td> <td data-index="2"></td> <td data-index="3"></td> <td data-index="4"></td></tr>
</table>
Without the support of :has you need to use JavaScript.
For this purpose you can use event delegation, which has the advantage that the data attributes and number of elements can change without the need to update the listeners.
let table = document.querySelector('table');
table.addEventListener('mouseover', (evt) => {
if ( evt.target.matches('[data-index]') ) {
table.dataset.hover = evt.target.dataset.index;
}
})
table.addEventListener('mouseout', (evt) => {
if ( evt.target.matches('[data-index]') ) {
table.dataset.hover = 0;
}
})
[data-hover="1"] [data-index="1"] {
background-color: red;
}
[data-hover="2"] [data-index="2"] {
background-color: red;
}
[data-hover="3"] [data-index="3"] {
background-color: red;
}
[data-hover="4"] [data-index="4"] {
background-color: red;
}
td {
width: 10px;
height: 10px;
border: 1px solid red;
}
td::before {
content: attr(data-index);
}
<table>
<tr> <td data-index="1"></td> <td data-index="2"></td> <td data-index="3"></td> <td data-index="4"></td></tr>
<tr> <td data-index="1"></td> <td data-index="2"></td> <td data-index="3"></td> <td data-index="4"></td></tr>
<tr> <td data-index="1"></td> <td data-index="2"></td> <td data-index="3"></td> <td data-index="4"></td></tr>
<tr> <td data-index="1"></td> <td data-index="2"></td> <td data-index="3"></td> <td data-index="4"></td></tr>
</table>
It can be done like below.
td[data-index="1"]:hover{
background-color: red;
}

Reset count for each table

I have multiple tables in my html. The tables have the columns with class "valuation" and valuation_all".
If one of the two cells in "valuation" contains FAIL, than the cell with "valuation_all" should change the text from "status" to "FAIL". Otherwise it should show PASS.
This works for one table, but I don't know how to get this for each table. I tried it with jQuery .each(".taglist") but its not working. I think I have to reset the variable count. For each table the variable should be resetted to zero and than start counting. Now the it keeps counting and does not change the status cell properly.
One of the tables: (other tables are identical just other ID)
<p>
<table id="results1" class="taglist">
<th>Name</th><th>result</th>
<tr>
<td class="valuation">FAIL</td><td class="valuation_all" rowspan=2>status</td>
</tr>
<tr>
<td class="valuation">PASS</td>
</tr>
</table>
</p>
jQuery:
$(document).ready(function() {
var count= 0;
$('table.taglist').each(
function() {
var count= $(".valuation:contains('FAIL')").length
if(count> 0) {
$(".taglist td:contains('status')").html("FAIL");
}else{
$(".taglist td:contains('status')").html("PASS");
}
});
});
Every help is appreciated!
You can use this to solve it.
$(document).ready(function() {
var count = 0;
$('table.taglist').each(
function() {
var count = $(".valuation:contains('FAIL')",this).length
$("td:contains('status')", this).html(count > 0 ? "FAIL" : "PASS");
});
});
The main problem is that your selectors is not referring to the table your are "inside". By doing $(".valuation:contains('FAIL')",this) you tell the selector (".valuation:contains('FAIL')") to search for this inside your table
Demo
$(document).ready(function() {
var count = 0;
$('table.taglist').each(
function() {
var count = $(".valuation:contains('FAIL')",this).length
$("td:contains('status')", this).html(count > 0 ? "FAIL" : "PASS");
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="results1" class="taglist">
<th>Name</th>
<th>result</th>
<tr>
<td class="valuation">FAIL</td>
<td class="valuation_all" rowspan=2>status</td>
</tr>
<tr>
<td class="valuation">PASS</td>
</tr>
</table>
<table id="results2" class="taglist">
<th>Name</th>
<th>result</th>
<tr>
<td class="valuation">PASS</td>
<td class="valuation_all" rowspan=2>status</td>
</tr>
<tr>
<td class="valuation">PASS</td>
</tr>
</table>
$(document).ready(function() {
$('table.taglist').each(function() {
let count= $(this).find(".valuation:contains('FAIL')").length;
$(this).find("td:contains('status')").html(count> 0 ? "FAIL" : "PASS");
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="results1" class="taglist">
<th>Name</th><th>result</th>
<tr>
<td class="valuation">FAIL</td><td class="valuation_all" rowspan=2>status</td>
</tr>
<tr>
<td class="valuation">PASS</td>
</tr>
</table>
<table id="results2" class="taglist">
<th>Name</th><th>result</th>
<tr>
<td class="valuation">PASS</td><td class="valuation_all" rowspan=2>status</td>
</tr>
<tr>
<td class="valuation">PASS</td>
</tr>
</table>
<table id="results3" class="taglist">
<th>Name</th><th>result</th>
<tr>
<td class="valuation">FAIL</td><td class="valuation_all" rowspan=2>status</td>
</tr>
<tr>
<td class="valuation">FAIL</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>

Add alternating table row colour based on date?

I have a table in html and the first column in each row contains a date. I want to give some sort of separation based on date. So all rows with todays date would be dark grey, yesterdays row would be light grey and then two days ago would be a grey again and so on.
Is there an easy way to do this?
Thanks
To have alternating rows a different colour (with multiple rows having the same date), you'll have to use jQuery to iterate through all the table rows to check whether it should color that row or not.
Below is the jQuery, HTML and CSS for an example.
// iterate over each row
var tableDate = $("#MyTable tbody").parents('tr:first').find('td:first').text();
var shouldColor = true
$("#MyTable tbody tr").each(function(i) {
// find the first td in the row
var value = $(this).find("td:first").text();
// display the value in console
if (value == tableDate) {
if (shouldColor == true) {
$('#MyTable tbody tr:nth-child(' + (i + 1) + ')').addClass("alternate");
}
} else {
if (shouldColor == false) {
shouldColor = true
$('#MyTable tbody tr:nth-child(' + (i + 1) + ')').addClass("alternate");
} else {
shouldColor = false
}
}
tableDate = value
});
#MyTable {
width:100%;
border-collapse:collapse;
}
#MyTable td {
padding:7px; border:blue 1px solid;
}
#MyTable tr {
background: light-grey;
}
#MyTable .alternate {
background: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<table id="MyTable" >
<tbody>
<tr>
<td>4/4/2016</td> <td>Running</td>
</tr>
<tr>
<td>4/4/2016</td> <td>Swimming</td>
</tr>
<tr>
<td>5/4/2016</td> <td>Running</td>
</tr>
<tr>
<td>6/4/2016</td> <td>Swimming</td>
</tr>
<tr>
<td>6/4/2016</td> <td>Swimming</td>
</tr>
<tr>
<td>6/4/2016</td> <td>Swimming</td>
</tr>
<tr>
<td>6/4/2016</td> <td>Swimming</td>
</tr>
<tr>
<td>7/4/2016</td> <td>Swimming</td>
</tr>
<tr>
<td>7/4/2016</td> <td>Swimming</td>
</tr>
<tr>
<td>8/4/2016</td> <td>Swimming</td>
</tr>
<tr>
<td>9/4/2016</td> <td>Swimming</td>
</tr>
<tr>
<td>10/4/2016</td> <td>Swimming</td>
</tr>
<tr>
<td>11/4/2016</td> <td>Swimming</td>
</tr>
<tr>
<td>11/4/2016</td> <td>Swimming</td>
</tr>
<tr>
<td>12/4/2016</td> <td>Swimming</td>
</tr>
<tr>
<td>12/4/2016</td> <td>Swimming</td>
</tr>
<tr>
<td>12/4/2016</td> <td>Swimming</td>
</tr>
<tr>
<td>13/4/2016</td> <td>Swimming</td>
</tr>
<tr>
<td>13/4/2016</td> <td>Swimming</td>
</tr>
<tr>
<td>13/4/2016</td> <td>Swimming</td>
</tr>
</tbody>
</table>