Need to access the <td> with the help of the classname - html

I need to apply styles to the entire row based on some condition check on the class name.
My table structure looks like:
<table>
<tbody>
<tr>
<td><div><span class='level2'></span></div></td>
<td></td>
<td></td>
</tr>
<tr>
<td><div><span class='level90'></span></div></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
I need to apply the styles to the rest of the <td> in a row that contains class name as 'level2' in the first <td>.
How do I traverse through the <tr> till the <span> and apply the styles to the <td>'s of same row?
I did try something:
var els = document.getElementById("ratiosTable").getElementsByClassName("Level2");
console.log(els);
for (var i = 0; i < els.length; i++) {
els[i].style.fontWeight = "bold"
}

OPTION 1
You can iterate the rows and check that it has a child of the expected class.
var rows = document.getElementById("ratiosTable").getElementsByTagName("tr");
console.log(rows);
for(var i = 0; i < rows.length; i++){
if(rows[i].getElementsByClassName("level2").length > 0) {
rows[i].style.fontWeight = "bold"
}
}
<table id="ratiosTable">
<tbody>
<tr>
<td>
<div><span class='level2'>Bold</span></div>
</td>
<td>B</td>
<td>B</td>
</tr>
<tr>
<td>
<div><span class='level90'>Not Bold</span></div>
</td>
<td>N</td>
<td>N</td>
</tr>
</tbody>
</table>
OPTION 2
This does not work on IE but does on all other major browsers including Edge. For more information : https://developer.mozilla.org/en-US/docs/Web/API/Element/closest
var els = document.getElementById("ratiosTable").getElementsByClassName("level2");
console.log(els);
for (var i = 0; i < els.length; i++) {
//NOTE closest
els[i].closest("tr").style.fontWeight = "bold"
}
<table id="ratiosTable">
<tbody>
<tr>
<td>
<div><span class='level2'>Bold</span></div>
</td>
<td>B</td>
<td>B</td>
</tr>
<tr>
<td>
<div><span class='level90'>Not Bold</span></div>
</td>
<td>N</td>
<td>N</td>
</tr>
</tbody>
</table>

Related

How can I sum up second column in html table using jquery

There is a table:
<table style="width:920px;" id="tblPotrawySkladniki">
<thead>
<tr>
<td class="cell0 style1"><p style="font-style:italic;">Nazwa produktu</p></td>
<td class="cell1 style1"><p style="font-style:italic;">Waga [g]</p></td>
</tr>
</thead>
<tbody>
<tr>
<td>banana</td>
<td>10</td>
</tr>
<tr>
<td>orange</td>
<td>20</td>
</tr>
<tr>
<td>raspberry</td>
<td>35</td>
</tr>
</tbody>
<tfoot>
<tr>
<td class="cell0 style1"><p style="font-weight:bold;">TOTAL</p></td>
<td class="cell1 style1"><p style="font-weight:bold;"></p></td>
</tr>
</tfoot>
</table>
How can I sum up all cells in tbody second column and put the result in tfoot cell also second column?
The table is dynamic. I would like to use jquery.
This would be one way of doing it:
let sum=$("#tblPotrawySkladniki tbody td:nth-child(2)").get().reduce((a,c)=>
+$(c).text().replace(",",".")+a,0);
$("#tblPotrawySkladniki tfoot td:nth-child(2)").text(sum);
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<table id="tblPotrawySkladniki">
<thead>
<tr>
<td class="cell0 style1"><p style="font-style:italic;">Nazwa produktu</p></td>
<td class="cell1 style1"><p style="font-style:italic;">Waga [g]</p></td>
</tr>
</thead>
<tbody>
<tr>
<td>banana</td>
<td>10</td>
</tr>
<tr>
<td>orange</td>
<td>20,3</td>
</tr>
<tr>
<td>raspberry</td>
<td>35,5</td>
</tr>
</tbody>
<tfoot>
<tr>
<td class="cell0 style1"><p style="font-weight:bold;">TOTAL</p></td>
<td class="cell1 style1"><p style="font-weight:bold;"></p></td>
</tr>
</tfoot>
</table>
Instead of parseInt() my script makes use of implicit type conversion with the unary + operator before $(c).text().
You can use jQuery .each() to cycle through the second td of each row, access the contents of the element using $(this).text(), then add them up as you go.
You should parse the value first so that they will add up as numbers and not concatenate.
let values = 0;
jQuery('table tbody tr td:nth-of-type(2)').each(function(){
let value = parseInt($(this).text(),10);
values += value;
});
console.log(values);
<table style="width:920px;" id="tblPotrawySkladniki">
<thead>
<tr>
<td class="cell0 style1">
<p style="font-style:italic;">Nazwa produktu</p>
</td>
<td class="cell1 style1">
<p style="font-style:italic;">Waga [g]</p>
</td>
</tr>
</thead>
<tbody>
<tr>
<td>banana</td>
<td> 10</td>
</tr>
<tr>
<td>orange</td>
<td> <span class='price'>20</span></td>
</tr>
<tr>
<td>raspberry</td>
<td> <span class='price'>35</span></td>
</tr>
</tbody>
<tfoot>
<tr>
<th colspan="2" style="text-align:center"><span id="sum"></span></th>
</tr>
</tfoot>
$(document).ready(function() {
$('.price').each(function(i) {
calculateColumn(i);
});
});
function calculateColumn(index) {
var total = 0;
$('table tr').each(function() {
var value = parseInt($('.price', this).eq(index).text());
if (!isNaN(value)) {
total += value;
}
});
$('#sum').eq(index).text('Total: ' + total);
}

The sum of the columns in the table (input)

I need to make a table with dynamic column sums on a website. Unfortunately, I'm not very good at jQuery. Please help.
[enter image description here][1]
My table in HTML
[1]: https://i.stack.imgur.com/yV2Fo.png
table, td {
border: 1.25px solid black;
}
<table>
<thead>
<tr>
<td> NAME</td>
<td> Name 1 </td>
<td> Name 2 </td>
<td> Name 3 </td>
</tr>
</thead>
<tbody>
<tr>
<td>A</td>
<td><input type="number"</td>
<td><input type="number"</td>
<td><input type="number"</td>
</tr>
<tr>
<td>B</td>
<td><input type="number"</td>
<td><input type="number"</td>
<td><input type="number"</td>
</tr>
<tr>
<td>C</td>
<td><input type="number"</td>
<td><input type="number"</td>
<td><input type="number"</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>Total</td>
<td></td>
<td></td>
<td></td>
</tr>
</tfoot>
</table>
I'm not sure how your table is set up. There are different ways you could do it, but I think this could be a solution:
I think something like this is what you are looking for?
Column
Total
A
150
B
100
C
50
Total
200
This is how you can accomplish this:
$(document).ready(() => {
let array = []; // Create an empty array
let sum = 0; // Set sum to 0 - This is only to set the data type to a number
/*
Use Jquery's :nth-child() selector to get target the 2nd cell in the table body
-- You can use :nth-child(3) for the 3rd cell etc...
Then use the $each method to perform a function.
The function will convert this cell's text (Using parseFloat() method) to a number and add (push()) it to our array
*/
$("table tbody td:nth-child(2)").each(function() {
array.push(parseFloat($(this).html()));
});
// Here we will loop over the array and add each value to the sum variable.
for (let i = 0; i < array.length; i++) {
sum += array[i];
}
// Then append the last child (last table cell) in the table's footer with this sum value || Or if you gave the this cell an id, you can use that instead.
$('table tfoot td:last-child').html(sum);
});
/* This can be used over many columns by selecting the different nth-child()'s
*/
table, td {
border: 1.25px solid black;
}
td {
padding: 3px;
text-align: center;
}
td:last-child {
font-weight: bolder;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<!-- Table Heading -->
<thead>
<tr>
<td> Column </td>
<td> Total </td>
</tr>
</thead>
<!-- Table Body -->
<tbody>
<tr>
<td>A</td>
<td>150</td>
</tr>
<tr>
<td>B</td>
<td>100</td>
</tr>
<tr>
<td>C</td>
<td>50</td>
</tr>
</tbody>
<!-- Table Footer -->
<tfoot>
<tr>
<td>Total </td>
<!-- Add an id to this cell for the total or use my method in the js code-->
<td id="tableTotal"></td>
</tr>
</tfoot>
</table>
Please read the comments in the snippet, this will explain the process.

Sum of <td> in dynamic Jquery

I have this table where I show in the td of white color is the consolidated information of the td of gray color.
Like this:
The yellow color fields "- Und" It should show the sum of the fields below.
The correct result would look something like this:
I have identified the tds by an id, but I can't get it to add up.
tdgray = gray
tdwhite = white
var total = 0;
$(".tdgray").each(function() {
total += parseInt($(this).text()) || 0;
});
$(".tdwhite").text(total);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<TABLE BORDER>
<TR>
<TD>price</TD>
</TR>
<TR>
<TD id="tdwhite" class="tdwhite"></TD>
</TR>
<TR>
<TD class="tdgray">421</TD>
</TR>
<TR>
<TD class="tdgray">124</TD>
</TR>
<TR>
<TD class="tdgray">982</TD>
</TR>
<TR>
<TD id="tdwhite" class="tdwhite">Und</TD>
</TR>
<TR>
<TD class="tdgray">200</TD>
</TR>
<TR>
<TD class="tdgray">200</TD>
</TR>
<TR>
<TD class="tdgray">200</TD>
</TR>
<TR>
<TD class="tdgray">200</TD>
</TR>
</TABLE>
If you see the result works but adds all the gray tds and does not go from record to record, I have a table with more than 300 white tds, can be a problem some solution?
LOOK THE EXAMPLE HERE
Your explanation of what you are needing is quite confusing.
I believe this is what you are looking for. It uses nextUntil() to loop through the rows that have a tdgray after each tdwhite and sums that group
$(".tdwhite").text(function() {
var total = 0;
$(this).parent().nextUntil(':has(.tdwhite)').each(function() {
total += parseInt($(this).find('.tdgray').text()) || 0;
});
return total
});
.tdwhite{color:red}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<TABLE BORDER>
<TR>
<TD>price</TD>
</TR>
<TR>
<TD id="tdwhite" class="tdwhite"></TD>
</TR>
<TR>
<TD class="tdgray">421</TD>
</TR>
<TR>
<TD class="tdgray">124</TD>
</TR>
<TR>
<TD class="tdgray">982</TD>
</TR>
<TR>
<TD class="tdwhite">Und</TD>
</TR>
<TR>
<TD class="tdgray">200</TD>
</TR>
<TR>
<TD class="tdgray">200</TD>
</TR>
<TR>
<TD class="tdgray">200</TD>
</TR>
<TR>
<TD class="tdgray">200</TD>
</TR>
</TABLE>
Here is another (Vanilla JavaScript) solution:
document.querySelectorAll(".tdwhite").forEach(el => {
let p, total = 0;
for (p = el.parentNode.nextElementSibling;
(p && !p.querySelector('.tdwhite')); p = p.nextElementSibling)
total += +p.querySelector('.tdgray').textContent || 0;
el.textContent = total
});
.tdwhite {
color: red
}
<TABLE BORDER>
<TR>
<TD>price</TD>
</TR>
<TR>
<TD id="tdwhite" class="tdwhite">one</TD>
</TR>
<TR>
<TD class="tdgray">421</TD>
</TR>
<TR>
<TD class="tdgray">124</TD>
</TR>
<TR>
<TD class="tdgray">982</TD>
</TR>
<TR>
<TD class="tdwhite">Und</TD>
</TR>
<TR>
<TD class="tdgray">200</TD>
</TR>
<TR>
<TD class="tdgray">200</TD>
</TR>
<TR>
<TD class="tdgray">200</TD>
</TR>
<TR>
<TD class="tdgray">200</TD>
</TR>
</TABLE>

How do you make a background image on a page scale appropriately with other page elements?

I'm trying to create a webpage that looks like a legal pad paper.
I want the text on the page to stay consistent with the background image, regardless of the size of the browser.
Currently, changing the size of the browser causes disproportion on the page.
Here are two photos that show the problem: http://imgur.com/a/6LvvH
How can I get around this? Thanks!
Currently, my CSS stylesheet looks like:
background-image: url(linktoimg);
background-position:center;
background-size:cover;
Perhaps instead of using an image, you could use a table. Format the table to have the colors of a legal pad.
Look at this jsfiddle i just whipped up to demonstrate the idea.
https://jsfiddle.net/zLmgybcv/
<style>
td.legalCell{
background-color: yellow;
width: 1000px;
height: 25px;
border-bottom: solid;
}
th.legalTop{
background-color: brown;
width: 1000px;
height: 150px;
}
}
</style>
<table>
<thead>
</thead>
<th class="legalTop"></th>
<tbody>
<tr>
<td class = "legalCell">
CONTENTS
</td>
</tr>
<tr>
<td class = "legalCell">
CONTENTS
</td>
</tr>
<tr>
<td class = "legalCell">
CONTENTS
</td>
</tr>
<tr>
<td class = "legalCell">
CONTENTS
</td>
</tr>
<tr>
<td class = "legalCell">
CONTENTS
</td>
</tr>
<tr>
<td class = "legalCell">
CONTENTS
</td>
</tr>
<tr>
<td class = "legalCell">
CONTENTS
</td>
</tr>
<tr>
<td class = "legalCell">
CONTENTS
</td>
</tr>
<tr>
<td class = "legalCell">
CONTENTS
</td>
</tr>
<tr>
<td class = "legalCell">
CONTENTS
</td>
</tr>
<tr>
<td class = "legalCell">
CONTENTS
</td>
</tr>
<tr>
<td class = "legalCell">
CONTENTS
</td>
</tr>
<tr>
<td class = "legalCell">
CONTENTS
</td>
</tr>
<tr>
<td class = "legalCell">
CONTENTS
</td>
</tr>
<tr>
<td class = "legalCell">
CONTENTS
</td>
</tr>
<tr>
<td class = "legalCell">
CONTENTS
</td>
</tr>
<tr>
<td class = "legalCell">
CONTENTS
</td>
</tr>
<tr>
<td class = "legalCell">
CONTENTS
</td>
</tr>
<tr>
<td class = "legalCell">
CONTENTS
</td>
</tr>
<tr>
<td class = "legalCell">
CONTENTS
</td>
</tr>
<tr>
<td class = "legalCell">
CONTENTS
</td>
</tr>
<tr>
<td class = "legalCell">
CONTENTS
</td>
</tr>
<tr>
<td class = "legalCell">
CONTENTS
</td>
</tr>
<tr>
<td class = "legalCell">
CONTENTS
</td>
</tr>
<tr>
<td class = "legalCell">
CONTENTS
</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>