How to Color code td element of HMTL table
I need to dynamically color code certain columns of table bases on figures in the cell. I want first 4 rowsto be formatted if they are more than 3 but all next rows should be formatted if figure in it is more than 1
I want to format alternate columns of table if for first 4 rows cell values is more than 3. 5th row onward i want to format if cell value is more than 1. I am using below javascript but that gives me all the rows formatting with 1
$(function()
{
$("td:nth-child(3),td:nth-child(5),td:nth-child(7),td:nth-child(9),td:nth-child(11)").each(function()
{
var score = parseFloat($(this).text());
if (score >= '1.00')
{
$(this).addClass('good');
}
else if (score < '-0.99')
{
$(this).addClass('good');
}
})
}
)
javascript html
add a comment |
I need to dynamically color code certain columns of table bases on figures in the cell. I want first 4 rowsto be formatted if they are more than 3 but all next rows should be formatted if figure in it is more than 1
I want to format alternate columns of table if for first 4 rows cell values is more than 3. 5th row onward i want to format if cell value is more than 1. I am using below javascript but that gives me all the rows formatting with 1
$(function()
{
$("td:nth-child(3),td:nth-child(5),td:nth-child(7),td:nth-child(9),td:nth-child(11)").each(function()
{
var score = parseFloat($(this).text());
if (score >= '1.00')
{
$(this).addClass('good');
}
else if (score < '-0.99')
{
$(this).addClass('good');
}
})
}
)
javascript html
2
You have converted score to number but compare it with a string. Why? You should compare same types, otherwise the automatic type conversion would take place as in your case.
– factor5
Dec 30 '18 at 5:28
So, what's in your table cells? I'd guess that they contain something which can't be parsed to floating point number and that's why you have this outcome.
– factor5
Dec 30 '18 at 5:47
thanks factor5 My cell has % value rounded to 2 decimals. The above code is working fine if i had to just compare for 1 % but i also want to compare first 4 rows to check 3%
– Suhas Jadhav
Dec 30 '18 at 7:07
add a comment |
I need to dynamically color code certain columns of table bases on figures in the cell. I want first 4 rowsto be formatted if they are more than 3 but all next rows should be formatted if figure in it is more than 1
I want to format alternate columns of table if for first 4 rows cell values is more than 3. 5th row onward i want to format if cell value is more than 1. I am using below javascript but that gives me all the rows formatting with 1
$(function()
{
$("td:nth-child(3),td:nth-child(5),td:nth-child(7),td:nth-child(9),td:nth-child(11)").each(function()
{
var score = parseFloat($(this).text());
if (score >= '1.00')
{
$(this).addClass('good');
}
else if (score < '-0.99')
{
$(this).addClass('good');
}
})
}
)
javascript html
I need to dynamically color code certain columns of table bases on figures in the cell. I want first 4 rowsto be formatted if they are more than 3 but all next rows should be formatted if figure in it is more than 1
I want to format alternate columns of table if for first 4 rows cell values is more than 3. 5th row onward i want to format if cell value is more than 1. I am using below javascript but that gives me all the rows formatting with 1
$(function()
{
$("td:nth-child(3),td:nth-child(5),td:nth-child(7),td:nth-child(9),td:nth-child(11)").each(function()
{
var score = parseFloat($(this).text());
if (score >= '1.00')
{
$(this).addClass('good');
}
else if (score < '-0.99')
{
$(this).addClass('good');
}
})
}
)
javascript html
javascript html
edited Dec 30 '18 at 5:22
brk
26.4k31940
26.4k31940
asked Dec 30 '18 at 5:21
Suhas JadhavSuhas Jadhav
62
62
2
You have converted score to number but compare it with a string. Why? You should compare same types, otherwise the automatic type conversion would take place as in your case.
– factor5
Dec 30 '18 at 5:28
So, what's in your table cells? I'd guess that they contain something which can't be parsed to floating point number and that's why you have this outcome.
– factor5
Dec 30 '18 at 5:47
thanks factor5 My cell has % value rounded to 2 decimals. The above code is working fine if i had to just compare for 1 % but i also want to compare first 4 rows to check 3%
– Suhas Jadhav
Dec 30 '18 at 7:07
add a comment |
2
You have converted score to number but compare it with a string. Why? You should compare same types, otherwise the automatic type conversion would take place as in your case.
– factor5
Dec 30 '18 at 5:28
So, what's in your table cells? I'd guess that they contain something which can't be parsed to floating point number and that's why you have this outcome.
– factor5
Dec 30 '18 at 5:47
thanks factor5 My cell has % value rounded to 2 decimals. The above code is working fine if i had to just compare for 1 % but i also want to compare first 4 rows to check 3%
– Suhas Jadhav
Dec 30 '18 at 7:07
2
2
You have converted score to number but compare it with a string. Why? You should compare same types, otherwise the automatic type conversion would take place as in your case.
– factor5
Dec 30 '18 at 5:28
You have converted score to number but compare it with a string. Why? You should compare same types, otherwise the automatic type conversion would take place as in your case.
– factor5
Dec 30 '18 at 5:28
So, what's in your table cells? I'd guess that they contain something which can't be parsed to floating point number and that's why you have this outcome.
– factor5
Dec 30 '18 at 5:47
So, what's in your table cells? I'd guess that they contain something which can't be parsed to floating point number and that's why you have this outcome.
– factor5
Dec 30 '18 at 5:47
thanks factor5 My cell has % value rounded to 2 decimals. The above code is working fine if i had to just compare for 1 % but i also want to compare first 4 rows to check 3%
– Suhas Jadhav
Dec 30 '18 at 7:07
thanks factor5 My cell has % value rounded to 2 decimals. The above code is working fine if i had to just compare for 1 % but i also want to compare first 4 rows to check 3%
– Suhas Jadhav
Dec 30 '18 at 7:07
add a comment |
1 Answer
1
active
oldest
votes
If what you're trying to do is to add styles to the table cells when the following conditions are met:
- For the first four rows, style the odd number column cells if the value is greater than or equal to 3
- For the fifth row and beyond, style the odd number column cells if the value is greater than or equal to 1
Then, you can add a class to the element for the first four rows like this: <tr class="firstFourRows">
For the javascript
$(document).ready(function () {
(function () {
/*
* choose either one (start)
*/
// if you CAN modify the HTML
// this selector select the first four tr elements with odd number column cells
$('.firstFourRows > td:nth-of-type(2n + 3)').each(function () {
var score = parseFloat($(this).text());
if (score >= 3) {
$(this).addClass('good');
}
});
// if you CANNOT modify the HTML
for (var i = 1; i <= 4; i++) {
$('tr:nth-child(' + i + ') > td:nth-of-type(2n + 3)').each(function () {
var score = parseFloat($(this).text());
if (score >= 3) {
$(this).addClass('good');
}
});
}
/*
* choose either one (end)
*/
// this selector select the fifth row and beyond with odd number column cells
$('tr:nth-child(n + 5) > td:nth-of-type(2n + 3)').each(function () {
var score = parseFloat($(this).text());
if (score >= 1) {
$(this).addClass('good');
}
});
}());
});
Hi @Ray Chan Thanks for reply. But my table is dynamically created with python script. That is why i can not add tr class <tr class="firstFourRows">. This is actually monthly report. If today is 5th of the month then table will show only 5 rows and so on so forth. I want to show % column in color format if first rows are more than 3% and 5th onward more than 1%. Can you suggest any solution for it.
– Suhas Jadhav
Dec 30 '18 at 19:28
I updated the answer, please let me know if it solves your problem.
– Ray Chan
Dec 31 '18 at 4:27
yes it did worked. Thanks for the help.
– Suhas Jadhav
Dec 31 '18 at 5:30
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53975457%2fhow-to-color-code-td-element-of-hmtl-table%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If what you're trying to do is to add styles to the table cells when the following conditions are met:
- For the first four rows, style the odd number column cells if the value is greater than or equal to 3
- For the fifth row and beyond, style the odd number column cells if the value is greater than or equal to 1
Then, you can add a class to the element for the first four rows like this: <tr class="firstFourRows">
For the javascript
$(document).ready(function () {
(function () {
/*
* choose either one (start)
*/
// if you CAN modify the HTML
// this selector select the first four tr elements with odd number column cells
$('.firstFourRows > td:nth-of-type(2n + 3)').each(function () {
var score = parseFloat($(this).text());
if (score >= 3) {
$(this).addClass('good');
}
});
// if you CANNOT modify the HTML
for (var i = 1; i <= 4; i++) {
$('tr:nth-child(' + i + ') > td:nth-of-type(2n + 3)').each(function () {
var score = parseFloat($(this).text());
if (score >= 3) {
$(this).addClass('good');
}
});
}
/*
* choose either one (end)
*/
// this selector select the fifth row and beyond with odd number column cells
$('tr:nth-child(n + 5) > td:nth-of-type(2n + 3)').each(function () {
var score = parseFloat($(this).text());
if (score >= 1) {
$(this).addClass('good');
}
});
}());
});
Hi @Ray Chan Thanks for reply. But my table is dynamically created with python script. That is why i can not add tr class <tr class="firstFourRows">. This is actually monthly report. If today is 5th of the month then table will show only 5 rows and so on so forth. I want to show % column in color format if first rows are more than 3% and 5th onward more than 1%. Can you suggest any solution for it.
– Suhas Jadhav
Dec 30 '18 at 19:28
I updated the answer, please let me know if it solves your problem.
– Ray Chan
Dec 31 '18 at 4:27
yes it did worked. Thanks for the help.
– Suhas Jadhav
Dec 31 '18 at 5:30
add a comment |
If what you're trying to do is to add styles to the table cells when the following conditions are met:
- For the first four rows, style the odd number column cells if the value is greater than or equal to 3
- For the fifth row and beyond, style the odd number column cells if the value is greater than or equal to 1
Then, you can add a class to the element for the first four rows like this: <tr class="firstFourRows">
For the javascript
$(document).ready(function () {
(function () {
/*
* choose either one (start)
*/
// if you CAN modify the HTML
// this selector select the first four tr elements with odd number column cells
$('.firstFourRows > td:nth-of-type(2n + 3)').each(function () {
var score = parseFloat($(this).text());
if (score >= 3) {
$(this).addClass('good');
}
});
// if you CANNOT modify the HTML
for (var i = 1; i <= 4; i++) {
$('tr:nth-child(' + i + ') > td:nth-of-type(2n + 3)').each(function () {
var score = parseFloat($(this).text());
if (score >= 3) {
$(this).addClass('good');
}
});
}
/*
* choose either one (end)
*/
// this selector select the fifth row and beyond with odd number column cells
$('tr:nth-child(n + 5) > td:nth-of-type(2n + 3)').each(function () {
var score = parseFloat($(this).text());
if (score >= 1) {
$(this).addClass('good');
}
});
}());
});
Hi @Ray Chan Thanks for reply. But my table is dynamically created with python script. That is why i can not add tr class <tr class="firstFourRows">. This is actually monthly report. If today is 5th of the month then table will show only 5 rows and so on so forth. I want to show % column in color format if first rows are more than 3% and 5th onward more than 1%. Can you suggest any solution for it.
– Suhas Jadhav
Dec 30 '18 at 19:28
I updated the answer, please let me know if it solves your problem.
– Ray Chan
Dec 31 '18 at 4:27
yes it did worked. Thanks for the help.
– Suhas Jadhav
Dec 31 '18 at 5:30
add a comment |
If what you're trying to do is to add styles to the table cells when the following conditions are met:
- For the first four rows, style the odd number column cells if the value is greater than or equal to 3
- For the fifth row and beyond, style the odd number column cells if the value is greater than or equal to 1
Then, you can add a class to the element for the first four rows like this: <tr class="firstFourRows">
For the javascript
$(document).ready(function () {
(function () {
/*
* choose either one (start)
*/
// if you CAN modify the HTML
// this selector select the first four tr elements with odd number column cells
$('.firstFourRows > td:nth-of-type(2n + 3)').each(function () {
var score = parseFloat($(this).text());
if (score >= 3) {
$(this).addClass('good');
}
});
// if you CANNOT modify the HTML
for (var i = 1; i <= 4; i++) {
$('tr:nth-child(' + i + ') > td:nth-of-type(2n + 3)').each(function () {
var score = parseFloat($(this).text());
if (score >= 3) {
$(this).addClass('good');
}
});
}
/*
* choose either one (end)
*/
// this selector select the fifth row and beyond with odd number column cells
$('tr:nth-child(n + 5) > td:nth-of-type(2n + 3)').each(function () {
var score = parseFloat($(this).text());
if (score >= 1) {
$(this).addClass('good');
}
});
}());
});
If what you're trying to do is to add styles to the table cells when the following conditions are met:
- For the first four rows, style the odd number column cells if the value is greater than or equal to 3
- For the fifth row and beyond, style the odd number column cells if the value is greater than or equal to 1
Then, you can add a class to the element for the first four rows like this: <tr class="firstFourRows">
For the javascript
$(document).ready(function () {
(function () {
/*
* choose either one (start)
*/
// if you CAN modify the HTML
// this selector select the first four tr elements with odd number column cells
$('.firstFourRows > td:nth-of-type(2n + 3)').each(function () {
var score = parseFloat($(this).text());
if (score >= 3) {
$(this).addClass('good');
}
});
// if you CANNOT modify the HTML
for (var i = 1; i <= 4; i++) {
$('tr:nth-child(' + i + ') > td:nth-of-type(2n + 3)').each(function () {
var score = parseFloat($(this).text());
if (score >= 3) {
$(this).addClass('good');
}
});
}
/*
* choose either one (end)
*/
// this selector select the fifth row and beyond with odd number column cells
$('tr:nth-child(n + 5) > td:nth-of-type(2n + 3)').each(function () {
var score = parseFloat($(this).text());
if (score >= 1) {
$(this).addClass('good');
}
});
}());
});
edited Dec 31 '18 at 4:26
answered Dec 30 '18 at 7:27
Ray ChanRay Chan
45418
45418
Hi @Ray Chan Thanks for reply. But my table is dynamically created with python script. That is why i can not add tr class <tr class="firstFourRows">. This is actually monthly report. If today is 5th of the month then table will show only 5 rows and so on so forth. I want to show % column in color format if first rows are more than 3% and 5th onward more than 1%. Can you suggest any solution for it.
– Suhas Jadhav
Dec 30 '18 at 19:28
I updated the answer, please let me know if it solves your problem.
– Ray Chan
Dec 31 '18 at 4:27
yes it did worked. Thanks for the help.
– Suhas Jadhav
Dec 31 '18 at 5:30
add a comment |
Hi @Ray Chan Thanks for reply. But my table is dynamically created with python script. That is why i can not add tr class <tr class="firstFourRows">. This is actually monthly report. If today is 5th of the month then table will show only 5 rows and so on so forth. I want to show % column in color format if first rows are more than 3% and 5th onward more than 1%. Can you suggest any solution for it.
– Suhas Jadhav
Dec 30 '18 at 19:28
I updated the answer, please let me know if it solves your problem.
– Ray Chan
Dec 31 '18 at 4:27
yes it did worked. Thanks for the help.
– Suhas Jadhav
Dec 31 '18 at 5:30
Hi @Ray Chan Thanks for reply. But my table is dynamically created with python script. That is why i can not add tr class <tr class="firstFourRows">. This is actually monthly report. If today is 5th of the month then table will show only 5 rows and so on so forth. I want to show % column in color format if first rows are more than 3% and 5th onward more than 1%. Can you suggest any solution for it.
– Suhas Jadhav
Dec 30 '18 at 19:28
Hi @Ray Chan Thanks for reply. But my table is dynamically created with python script. That is why i can not add tr class <tr class="firstFourRows">. This is actually monthly report. If today is 5th of the month then table will show only 5 rows and so on so forth. I want to show % column in color format if first rows are more than 3% and 5th onward more than 1%. Can you suggest any solution for it.
– Suhas Jadhav
Dec 30 '18 at 19:28
I updated the answer, please let me know if it solves your problem.
– Ray Chan
Dec 31 '18 at 4:27
I updated the answer, please let me know if it solves your problem.
– Ray Chan
Dec 31 '18 at 4:27
yes it did worked. Thanks for the help.
– Suhas Jadhav
Dec 31 '18 at 5:30
yes it did worked. Thanks for the help.
– Suhas Jadhav
Dec 31 '18 at 5:30
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53975457%2fhow-to-color-code-td-element-of-hmtl-table%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
You have converted score to number but compare it with a string. Why? You should compare same types, otherwise the automatic type conversion would take place as in your case.
– factor5
Dec 30 '18 at 5:28
So, what's in your table cells? I'd guess that they contain something which can't be parsed to floating point number and that's why you have this outcome.
– factor5
Dec 30 '18 at 5:47
thanks factor5 My cell has % value rounded to 2 decimals. The above code is working fine if i had to just compare for 1 % but i also want to compare first 4 rows to check 3%
– Suhas Jadhav
Dec 30 '18 at 7:07