What is the process for altering the color of an HTML output depending on its values?

I created a simple HTML code to showcase some outcomes.

The possible results are SUCCESS, Failure, and Still Failing. I want these results to be displayed with corresponding colors, such as green for SUCCESS, and red for both Failure and Still Failing.

I searched for solutions online, but most of them involve using JavaScript or jQuery which I am not familiar with. I attempted various methods without success.

Is there an "if" condition statement that could handle this situation?

    <body leftmargin="8" marginwidth="0" topmargin="8" marginheight="4" offset="0">
        <table width="95%" cellpadding="0" cellspacing="0" style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif">
            <tr>
                <td>(Automatic email, DO NOT REPLY)</td>
            </tr>
            <tr>
                <td>
                    <h2>
                        <font color="#039b10">Build result - SUCCESS</font>
                    </h2>
                </td>
            </tr>
        </table>
    </body>

    </html>

Answer №1

It's important to note that the use of the <font> tag is considered outdated and it's recommended to utilize CSS classes instead. Consider assigning a class like "success" or "failure" to your heading tags (<h2>) and then define the appropriate styles in your CSS file.

If you're new to CSS, I highly recommend checking out resources such as this guide on Mozilla Developer Network to kickstart your learning journey.

Answer №2

If you want to dynamically change the CSS based on a value, take a look at this code snippet.

When you input the word success in lowercase, the background will turn green; otherwise, it will be red.

$('#inputButton').click(function(){
  var inputVal = $('#inputText').val();
  if(inputVal == 'success'){
    $('#message').removeClass('red');
    $('#message').addClass('green');
  } else {
    $('#message').removeClass('green');
    $('#message').addClass('red');
  }
});
.green{
background : green;
}
.red{
background : red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="inputText" />
<input type="button" id="inputButton" value="Check" />
<div id="message">Message</div>

Answer №3

Imagine having a container called build_results to display results:

<div id='build_results'></div>

Now, picture a script like the one below:

<script>
//Assuming the use of jQuery
function GetResultsFromWork() {
  //Perform some work
  return resultOfWork;
}    

function someScript(){
  var workResults = GetResultsFromWork();
  setBuildResponseMessage(workResults);  
}

function setBuildResponseMessage(results) {
  switch(results) {
    case 'failure':
    case 'failure_again':
      $('#build_results')
        .removeAttr('class')
        .addClass('buildFailure')
        .text('Failure!');
    case 'success':
    default:
      $('#build_results')
        .removeAttr('class')
        .addClass('buildSuccess')
        .text('Success!');
      break;
  }
}
</script>

Also, suppose you have defined the following CSS styles:

<style>
.buildFailure {
  color:red;
}

.buildSuccess {
  color:green;
}
</style>

This is just a rough outline. While it has been a while since I wrote JavaScript, this should function properly based on initial observations.

Answer №4

By adding a class to a font tag, you have the ability to adjust the color of that class based on your desired outcome.

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

Tips on setting the default sorting order in AngularJS

I have implemented a custom order function in my controller with the following code: $scope.customOrder = function (item) { var empStatus = item.empState; switch (empStatus) { case 'Working': return 1; case & ...

A guide on loading theme JavaScript files in Next.js

I am planning to incorporate a theme from themeforest into Next.js Despite my attempts, I encountered issues with loading jquery core and certain plugins in Nextjs. import { useEffect } from "react" import { SessionProvider } from "next-aut ...

Guide for using two Async Pipe functions in Angular 7

Two different functions are in place to check a specific condition, and the requirement is for both of them to be true simultaneously. How can *ngIf be utilized to achieve this? Currently, setting just one of them works, but the aim is to have both. HTML ...

Assistance with offsetting a jQuery drop down menu

This is the third jQuery script that I've been working on. While parts of it have been inspired by other scripts, I'm now focusing on implementing a specific feature. I've dedicated 4 hours to solving the issue of displaying the submenu on ...

Tips for including MUI icon within a list displayed on a map:

Initially, I brought in the AccountCircle Icon from MUI: import { AccountCircle } from '@mui/icons-material'; Then, I utilized styled to customize the icon: const UserIcon = styled(AccountCircle)({ margin: '0px 0px 0px 0px', }); My ex ...

Restrict the number of items in each list to just one

I'm trying to customize a query that displays a list of numbers. My goal is to only display each unique number once. For example, if there are three instances of the number 18 in the database, I want it to show as 18, 18, 18. If there are two occurre ...

Does anyone have an idea of the origin of the item in this ajax .each function?

Currently, I am utilizing the Etsy API with JavaScript by calling this AJAX code: $.ajax({ url: etsyURL, dataType: 'jsonp', success: function(data) { This code returns an object array, if I'm not mistaken. It then proceeds to enter this . ...

Picture goes missing from slideshow

I am currently using a CSS + Javascript slideshow on my website, inspired by an example from the W3Schools website. Here is the code I have adapted for my web application: $(document).ready(function() { var slideIndex = 1; function showSlides(n) { ...

The React component continuously refreshes whenever the screen is resized or a different tab is opened

I've encountered a bizarre issue on my portfolio site where a diagonal circle is generated every few seconds. The problem arises when I minimize the window or switch tabs, and upon returning, multiple circles populate the screen simultaneously. This b ...

Ways to dynamically display a button on a JQuery table

Deleting rows in a table can be done by clicking on the Remove Button. However, it is important to note that at least one row must always be present. To ensure this, I am checking the length of the table: if($("#dynamicTable1 tr").length==2) { ...

Choose the radio button upon clicking away from the input field

Currently, I have a standard Bootstrap 4 accordion that contains a radio button. Here is the code snippet: <div class="card"> <div class="card-header" id="headingOne"> <h2 class="mb-0"> ...

React filtering displaying array elements that appear single time

I've been working on this React code to filter and search items based on user input. The issue I'm facing is that when I delete the text input and try again, the filtered items disappear and don't show up unless I reload the page. I'm c ...

Should all variables be retrieved from the database and stored as session variables, or is this considered a poor practice?

I implemented the jQuery load function to only refresh the body content of my website, while keeping the header consistent. Instead of repeatedly querying the database for the same information across multiple pages, could I potentially increase the initi ...

Having trouble retrieving JSON data from an external URL in AngularJS when making a $http.get call and using the success method?

Code in the Controller.js file: let myApp=angular.module('myApp',[]); myApp.controller('myController', function($scope,$http){ $http.get('data.json').success(function(data){ $scope.art=data; }); }); ...

What is the method to retrieve the value from $.get()?

After searching on stackoverflow, I was unable to locate a solution to my issue and am currently unable to comment for further assistance. The dilemma I face involves extracting a return value from an asynchronous method to pass it onto another function: ...

Creating a tree-view in Vue.js that includes clickable components which trigger a Vue.js modal to open up

I have a unique requirement to implement a tree-view feature in Vue-JS for displaying JSON data. However, I need to enhance this by triggering a VueJS modal when any of the data fields in the JSON view are clicked. I have explored various npm modules that ...

Creating a gaming application with Phaser.js and Ionic with subpar rendering capabilities

Attention developers! I recently created a game app using Phaser.js. I integrated the code into an Ionic blank starter app, allowing the Ionic framework to render the view while Phaser takes care of displaying the game. Issue: The game is a simple flapp ...

The Fusion of JavaScript Frameworks

Is it considered poor practice for a seasoned developer to build a web application using multiple JS frameworks? For instance, when incorporating AngularJS into a project, and certain tasks could be more efficiently achieved with JQuery, should one opt fo ...

Display a single image on the tablet and a distinct image on the computer

Currently, I am encountering an issue with my webpage located at . The problem lies in the right upper corner where a ribbon is located. However, when viewing the page on a tablet, the ribbon overlaps with my menu. To resolve this, I thought about displa ...

SEO Optimized pagination for pages without modifying the URL

On my website, I utilize pagination to navigate to various event pages. However, search engines are not picking up the conferences on these pages. Take a look at the code snippet below... <a href="javascript:;" class="first" title="First" onclick="getC ...