Style table cells dynamically based on their content in HTML

As a complete beginner in this field, I am currently working on a simple xslt presentation file that includes a table. I want to customize the background color of either a cell or an entire row based on its content.

For example, if the rating is "GOOD", "MEDIUM" or "BAD", I would like the background color to be green, yellow, or red respectively.

This is my current table code:

<table width="1000" border="0" cellspacing="0" cellpadding="0" class="table-fill">
    <thead>
        <tr>
            <th scope="col">NAME</th>
            <th scope="col">VALUE</th>
            <th scope="col">RATING</th>
            <th scope="col">DESCRIPTION</th>
        </tr>
    </thead>
    <tbody>
        <xsl:for-each select="test/criteria">
            <tr>
                <td><xsl:value-of select="@nom" /></td>
                <td><xsl:value-of select="value" /></td>
                <td><xsl:value-of select="rating" /></td>
                <td><xsl:value-of select="descr" /></td>
            </tr>
        </xsl:for-each>
    </tbody>
</table>

Your help is greatly appreciated.

--UPDATE-- Special thanks to @Yaakov Ainspan for answering my question, and @Ruud for providing the necessary code snippets.

This is not a duplicate of "Is there a CSS selector for elements containing certain text?" because I was focused on creating classes with names matching the content of a specific cell, rather than isolating elements from strings with multiple contents.

Answer №1

If you are dealing with only those specific three values, consider creating classes in your stylesheet that correspond to each value:

.GOOD {
    background-color: green;
}
.MEDIUM {
    background-color: yellow;
}
.BAD {
    background-color: red;
}

Then, you can either add the class attribute to the row like this:


    <xsl:for-each select="test/criteria">
        <tr>
            <xsl:attribute name="class">
                <xsl:value-of select="rating" />
            </xsl:attribute>
            <td><xsl:value-of select="@nom" /></td>
            <td><xsl:value-of select="value" /></td>
            <td><xsl:value-of select="rating" /></td>
            <td><xsl:value-of select="descr" /></td>
        </tr>
    </xsl:for-each>

Alternatively, you can add the class attribute directly to the cell:


    <xsl:for-each select="test/criteria">
        <tr>
            <td><xsl:value-of select="@nom" /></td>
            <td><xsl:value-of select="value" /></td>
            <td>
                <xsl:attribute name="class">
                    <xsl:value-of select="rating" />
                </xsl:attribute>
                <xsl:value-of select="rating" />
            </td>
            <td><xsl:value-of select="descr" /></td>
        </tr>
    </xsl:for-each>

You also have the option to improve readability by adding the class in a different way:


    <xsl:for-each select="test/criteria">
        <tr>
            <td><xsl:value-of select="@nom" /></td>
            <td><xsl:value-of select="value" /></td>
            <td class="{rating}"><xsl:value-of select="rating" /></td>
            <td><xsl:value-of select="descr" /></td>
        </tr>
    </xsl:for-each>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the difference between finding the XPath of immediate child text and all descendant text?

Looking to extract the top-level category from this structure: <ul class="categories"> <li>My top level category <ul> <li>my second level category</li> </ul> </li> </ul> ...

Troubleshooting CSS Problems: Issues with background repeating and footer placement

I am currently in the process of transitioning my website to a CSS layout, and so far it's looking good. However, I am facing two specific issues with the page layout that I need help with: 1) The background image of the left-most column is not repea ...

Guide on capturing JSON objects when the server and client are both running on the same system

I created an HTML page with a form that triggers JavaScript when submitted using the following event handler: onClick = operation(this.form) The JavaScript code is: function operation(x) { //url:"C:\Users\jhamb\Desktop\assignmen ...

Change the location of an html td element with JavaScript

I am currently working on a simple JavaScript car racing game where the cars are represented by HTML table elements. I want to implement functionality where pressing the "e" key moves player one's car and the "d" key moves player two's car. This ...

Are there any tools you rely on for speeding up the process of developing html/css?

Have you heard of zen-coding? It's a script that generates markup based on a css-esque selector like this: div#foo > p*6 This code would generate: <div id="foo"> <p></p> <p></p> <p></p> & ...

What could be the reason my code isn't successfully performing addition within the input field?

As a novice, I am practicing by attempting to retrieve a number from a text field, prompting the user to click a button that adds 2 to that number, and then displaying the result through HTML. However, I keep encountering an issue where NaN is returned whe ...

Incorporating .json files into an HTML template with the help of an HTML form designed for selecting a particular

I am faced with a collection of various .json files that I wish to specify by name on an HTML page (local) form, enabling me to load this data onto a table. This is how my HTML form appears: <form> File: <input type="text" Id="file_name"&g ...

Switch out the HTML content within the JavaScript include

Here is the situation: <script type="text/javascript" src="http://xy.com/something.js"></script> This code snippet in my PHP/HTML file loads the entire something.js file. Inside that file, there are lines containing "document.write..." which ...

"Tips for stopping users from using Ctrl+U to view page source in

Is there a way to prevent users from viewing the source code (HTML + JavaScript) of a page by disabling Ctrl+U in the browser? ...

What is the best way to allow CORS in an internet server by utilizing AJAX and PHP in order to obtain a font to be used on a different BigCart

Currently, I am using BigCartel for website design. I am looking to incorporate a custom font into my design. However, regardless of the server I use (currently a free Hostinger server), I am unable to enable CORS using htaccess. Recently added to .htacc ...

Have you ever wondered how the automatic video play on scroll feature works on Tiktok.com and YouTube shorts when using a mobile device?

My goal with React JS is to develop a website similar to Tiktok, where the video below will automatically play with sound as the user scrolls down. I attempted to set this up using window.addEventListener("scroll",...), but after looking into it further, ...

Execute a function when a button is pressed in a React application

Currently, I am dynamically generating some HTML and have a requirement for certain "events" to trigger an onclick function. The technology stack I am using for this project involves React and TypeScript. My initial approach is as follows: function add_ev ...

Tips for avoiding anti-aliasing of bitmap font on Internet Explorer and Firefox

I am currently experiencing issues with font smoothing when using a basic bitmap font for icons. While the font appears crisp in Chrome, it appears blurry in IE and FF. Do you have any recommendations on how to resolve this problem? You can view a screen ...

The disabled button is not displayed when it is disabled

Encountering an issue specific to Chrome version 60.0.3112.90 where changing the disabled attribute of a button causes it to disappear. A minimal, complete, and verifiable example has been prepared wherein clicking the + button toggles the disabled attri ...

The functionality to open the menu by clicking is experiencing issues

I'm attempting to replicate the Apple website layout. HTML structure: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" conte ...

Placing an image on the chosen option of a <mat-select> using Angular Material

I have incorporated Angular Material into my Angular 2 project, and I am trying to insert a static image (HTML element) in the selected value of mat-select. Unfortunately, I have been unable to find a solution for this issue. Is there anyone who can ...

Data not being displayed in dynamic <table>

Need help creating a dynamic table that displays data inserted in a specific format. {headers: ["header1", "header2"], rows: [["data1", "data2"], ["moredata", "wowoso muchdata"]]} The 'rows' consists of arrays where each array represents a row ...

Is there a way to automatically scroll two identical vertical long images one after the other in a continuous loop?

My goal is to replicate the effect seen on the website , where a vertical image moves downwards and then repeats by starting from the bottom once it reaches the top. Essentially, there are two images moving downward in this loop. I attempted to find a sui ...

Icon that can be clicked before

I'm struggling to figure out how to make the link icon in my code clickable so that users can click anywhere within the card to navigate to another page. I experimented with (" attr(href) ") but it resulted in adding the URL text next to the ...

Need to swiftly modify values of css attributes?

Here is a snippet of the code I am working with: <div></div> <button> Go </button> div { width: 50px; height: 50px; border: 1px solid #ccc; } var bgs = ['red', 'blue', 'yellow', 'green&apo ...