Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I conditionally format my HTML table?

<td Class='metric' title='Test gave a performance metric.' lastPassTag=''>1997.0</td>
<td Class='metric' title='Test gave a performance metric.' lastPassTag=''>1997.0</td>
<td Class='metric' title='Test gave a performance metric.' lastPassTag=''>1997.0</td>
<td Class='metric' title='Test gave a performance metric.' lastPassTag=''>1997.0</td>
</tr><tr class='detail-hide'><td Class='result-name '>pmu: COMMITTED_PKT_BSB</td>
<td Class='metric' title='Test gave a performance metric.' lastPassTag=''>1655.0</td>
<td Class='metric' title='Test gave a performance metric.' lastPassTag=''>1836.0</td>
<td Class='metric' title='Test gave a performance metric.' lastPassTag=''>1655.0</td>
<td Class='metric' title='Test gave a performance metric.' lastPassTag=''>1836.0</td>

I have a HTML table like above I'm trying to do conditional formatting based on the formula applied on the numbers there I tried this:

var tb = document.getElementByClass('metric')

I could not get those values Any modifications or suggestions are appreciated Thank you

like image 244
Ganga Avatar asked Mar 07 '19 04:03

Ganga


People also ask

How do you add conditional formatting in HTML?

Conditional rendering of HTML is not a new concept, but it cannot be done using HTML exclusively. You would need to use either client side scripting or server side code to provide the conditional logic that would render your HTML accordingly.

How do you create a conditional table?

Select the desired cells for the conditional formatting rule. From the Home tab, click the Conditional Formatting command. A drop-down menu will appear. Hover the mouse over the desired conditional formatting type, then select the desired rule from the menu that appears.

How do you format a table in HTML?

To create table in HTML, use the <table> tag. A table consist of rows and columns, which can be set using one or more <tr>, <th>, and <td> elements. A table row is defined by the <tr> tag. To set table header, use the <th> tag.

How do you color code a table in HTML?

In HTML, table background color is specified using Cascading Style Sheets (CSS). In particular, you use the CSS background-color property to set the background color for your table. You can also specify a separate background color for your table rows and table cells if you like.


Video Answer


2 Answers

the only problem with your code is you are using wrong js context to search for class using js.

document.getElementByClass('metric')

as classes can be more then 1 so the context to select class is having elements instead of element like below It should be Elements(Plural) not Element(Singular)

document.getElementsByClass('metric')

hope this will solve your query.

if need any other help, just comment here I will try to solve

like image 79
Nitin Garg Avatar answered Sep 22 '22 21:09

Nitin Garg


The method is wrong - you want to use document.getElementsByClassName:

var tb = document.getElementByClass("metric");

You could also use querySelectorAll to only get td elements with the class metric:

var tb = document.querySelectorAll("td.metric");
like image 27
Jack Bashford Avatar answered Sep 24 '22 21:09

Jack Bashford