Styling text using CSS depending on the displayed text

Utilizing Awesome Tables (AT), I am extracting data from a Google Sheets document to display on a website. The HTML template in the sheets is used for formatting the data, specifically focusing on the table output with inline CSS styling. Since the template only includes the <body> element, access to other sections like <head> is restricted.

One particular piece of data (${"Status"}) retrieved from Google Sheets can have three potential values: Active, Delivered, or Cancelled. The output is presented as:

<div style="display:inline-block;color:rgb(87, 87, 87);font-size:14px;padding:10px 10px 10px 0;flex-shrink:0;margin-right:auto;text-transform:capitalize;">
      <p><b>Name:</b> ${"Name"}</p>
      <p><b>Start Date:</b> ${"Start Date"}</p>
      <p><b>Completed Date:</b> ${"Completed Date"}</p>
      <p><b>Status:</b> ${"Status"}</p>
</div>

The goal is to color code the text based on the value of ${"Status"} - "Active" = orange, "Cancelled" = red, and "Delivered" = green. While considering using JavaScript for this task, guidance on how to proceed is requested.

Any assistance would be greatly appreciated.


Based on the feedback received, here is an initial attempt at writing JavaScript following some research. Is this approach heading in the right direction?

var jobStatus = "${"Status"}";

if (jobStatus === "Delivered") {
  document.getElementById("status").style.color = "green";
} else if (jobStatus === "Active") {
  document.getElementById("status").style.color = "orange";
} else {
  document.getElementById("status").style.color = "red";
}

Answer №1

To enhance the appearance of your HTML template, I recommend utilizing CSS classes for applying colors. By assigning the value as a CSS class and adding some custom CSS styling, you can format your text or content to your liking:

<div style="display:inline-block;color:rgb(87, 87, 87);font-size: 14px;padding: 10px 10px 10px 0;flex-shrink: 0;margin-right: auto;text-transform: capitalize;">
  <p><b>Name:</b> ${"Name"}</p>
  <p><b>Start Date:</b> ${"Start Date"}</p>
  <p><b>Completed Date:</b> ${"Completed Date"}</p>
  <p class="${"Status"}"><b>Status:</b> ${"Status"}</p>
</div>

The provided CSS code is static and can be placed in your document's header section. If dynamic access is not available (as per my understanding), using JavaScript to append it to the body could also be a suitable solution.

.Active {
   color: orange;
}
.Cancelled {
   color: red;
}
.Delivered {
   color: green;
}

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

Navigating with Vue Router in a Nuxt JS vuex Store

In my app, I'm currently developing a login/register system using Firebase and Vuex for authentication and database management. Within Nuxt JS, I have implemented actions to create a user, log them in, and sign them out. After a successful registrati ...

How can I use jQuery to automatically redirect to a different HTML page after updating a dynamic label?

When I click on a button in first.html, a function is called to update labels in second.html. However, despite being able to see second.html, I am unable to see the updated values in the labels. Can someone please advise me on how to achieve this? functi ...

Guide on integrating angular-schema-form into an Ionic 2.0 project using typescript

Recently, I embarked on creating an app with Ionic from scratch and decided to integrate the framework. While I faced no issues executing the example on a webpage, I encountered difficulties when attempting to do so with Ionic. To kickstart the project, ...

When attempting to access endpoints from other computers, the connection to Express.js is being refused

I have set up an Express server and React for the frontend. The express server is running on port 5000 and React on port 3000. Additionally, I am using JWT tokens for authentication. When I login to the app from the computer it is running on, everything w ...

Difficulty encountered when trying to use Bootstrap tooltip with Bootstrap icon

Attempting to implement bootstrap tooltips on bootstrap icons with the following code: <body> <script> const tooltipTriggerList = document.querySelectorAll('[data-bs-toggle="tooltip"]') ...

Activate the toggle feature on the navigation bar using Bootstrap

Below is the code I am using: <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8defe2e2f9fef9ffecfdcdb8a3bda3bf">[email protected]</a>/dist/css/bootstrap.min.css" rel ...

What is the best way to ensure that the text within Span children is properly laid out to match the width of the parent div tag?

This particular instance involves setting the width of the div tag to 200px and organizing all the span children to occupy the entire width of the parent div tag. If the first row is filled, the span tags should then flow into a second row. My attempt at ...

Every time I refresh the page, the user is automatically logged out

I am currently working on developing an admin dashboard using nextjs 13. I have encountered a specific issue where the user is redirected to the login page every time they reload the page. Upon inspecting in developer mode, I noticed that cookies are still ...

Switching from "Straight Quotes" to "Curly Quotes"

Looking for a solution to convert regular straight quotes into curly quotes in a Javascript-based rules engine application. Rather than just using a simple string.replace for ["], I need a method that will insert the correct curly quote depending on its po ...

Having trouble with the sx selector not functioning properly with the Material UI DateTimePicker component

I'm currently working with a DateTimePicker component and I want to customize the color of all the days in the calendar to match the theme color: <DateTimePicker sx={{ "input": { color: "primary.main&quo ...

Ways to update modal content upon clicking a button

I have a scenario where I need to display 2 modals (box1 and box2) in my code with box2 appearing on top of box1. Each modal has its own button, and I want to make sure that box1 appears first. Then, when the user clicks the button in box1, it transitions ...

Loading static assets in Express.js

I am currently utilizing express for developing a web application. However, I am encountering issues with my routes and static files. I have included a reference to static files: app.use(express.static(path.join(__dirname, 'public'))); and conf ...

Buttons with a slight lean towards the edge

The alignment of these buttons within their container is not what I desire. They are currently floating to the left, which is a result of the float: left; attribute applied to them. I tried removing the float: left; and instead used text-align: center; on ...

Tips for implementing focusout on multiple elements while avoiding it when switching focus between them

Looking to create a searchable input that displays a dropdown of possible values on focusin, populated by server-side language. Attempted using focusin on elements not involved with the following code: $("body").not($(".dropdownSearch") ...

Saving a Session Variable when a button is clicked in HTML and PHP

I'm struggling with storing Session variables when a button is clicked. I want to achieve this without using a form. Below is the code I currently have for my HTML and PHP. <?php session_start(); if(isset($_GET['oldsignin'])) { $user ...

Injecting resolve into Angular controller and encountering undefined value in logging operation

My setup involves the following: .state('posts', { url: '/posts/{id}', templateUrl: 'posts.html', controller: 'postsController as postsCtrl', resolve: { post: getSinglePostWrapper ...

What is the best way to handle waiting for a React context provider that requires time to initialize the value it provides?

In my Next.js application, I have a global context that takes five seconds to compute the value provided: import React, { useContext, useEffect, useState } from 'react'; const GlobalContext = React.createContext(); export const GlobalContextPro ...

filtering out specific sections of HTML using xPath

I am currently attempting to scrape information from this page My approach involves using xPath to select specific elements. Here is a snippet of my code: $safeFlag = true ; //*[@id="tabset_productPage"]/dd[1]/div/div //HAVE TRIED THIS TOO //*[@id="tab ...

Troubleshooting Problem with Angular JS Page Loading on Internet Explorer 9

My angularjs single page application is encountering issues specifically in IE9, despite functioning perfectly in Chrome and Firefox. The initial load involves downloading a substantial amount of data and managing numerous dependencies through requireJS. ...

The jQuery.ajax request encounters issues within a Chrome extension

I'm in the process of migrating one of my Firefox browser extensions to Chrome, and I've encountered an issue related to an AJAX query. The code snippet provided functions correctly in the Firefox extension but fails with a status of "0" when exe ...