Changing the text color using jQuery is proving to be difficult for me

I've been trying to apply a styling condition using an if statement in my jQuery code based on the result from an API, but for some reason, I'm not seeing the color change. I have given an ID to try and change the color through CSS.

$.getJSON(API2, function (result) {
  console.log(result);

  for (i = 0; i < result.length; i++) {
    var tabledata = `
      <tbody>
        <tr>
          <th scope="row">`+ result[i].Season + `</th>
          <td><p class="td-item">`+ result[i].DateTime + `</p></td>
          <td><p class="td-item">`+ result[i].Name + `</td>
          <td><p class="td-item" class="stat">`+ result[i].Status + `</p></td>

        </tr>
    `;

    var stat = result[i].Status;
    $(".event").append(tabledata);
  }

  if (stat == "final") {
    $(".td-stat").attr('id', 'td-stat');
    console.log(stat);
  }
});

Answer №1

Take a look at this scenario.

$.getJSON(API2, function(result) {
  console.log(result);
  $("<tbody>").appendTo(".event");
  $.each(result, function(i, r) {
    var row = $("<tr>").appendTo($(".event tbody"));
    $("<th>", {
      scope: "row"
    }).html(r.Season).appendTo(row);
    $("<td>").html($("<p>", {
      class: "td-item"
    }).html(r.DateTime)).appendTo(row);
    $("<td>").html($("<p>", {
      class: "td-item"
    }).html(r.Name)).appendTo(row);
    $("<td>").html($("<p>", {
      class: "td-item stat " + (r.Status == "final" ? "final" : "")
    }).html(r.Status)).appendTo(row);
  });
});

This piece of code generates a tbody element and fills it with rows based on the data in result.

Answer №2

To properly manipulate elements on a webpage, it is important to assign them an ID or retrieve them as an array and then loop through them. For changing the color of an element, you can achieve this using the following code snippet:

$("#yourElementId").css('color', 'blue');

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

Issue with cancelling a file upload is not functioning properly on Internet Explorer

My experience with file uploads in Internet Explorer is causing some issues. While other browsers work smoothly when canceling file uploads, Internet Explorer seems to have a different behavior. In other browsers, clicking the "Cancel" button during a fil ...

Tips for selectively applying CSS to a single HTML file

I'm currently working on a website utilizing AngularJS and Plangular to develop a unique Soundcloud Player. This is how I incorporate my template: <body> <div id="container"> <div id="header" ng-controller="HeaderCtrl"><div ...

Ways to adjust the brightness of any color when hovered over

Is it possible to create a universal effect where an element becomes darker or lighter when hovered over, regardless of the initial color? Here is an example: .change-color{ Background:green; width:100px; height:100px } .change-color:hover{ Background ...

What is preventing me from being able to manipulate the data retrieved from this ajax request?

I've been attempting to make an ajax call to my json server on localhost:3000 in order to retrieve an object that I can manipulate and display on a webpage. However, no matter what I try, I just can't seem to console log the (data) so that I can ...

Set the height of the main div container to a fixed size

I am facing an issue with my div container (mapInfo-Container) which contains multiple div blocks. I want the container to have a fixed height of 250px, and be scrollable for any content exceeding this height. Here is the code snippet: http://jsfiddle.net ...

section featuring a striking visual, user input form, and descriptive content

I want to create a layout with a background image in a div, a signup form on the right side, and a div containing h1 tags on the left side. Here is the HTML code: <html> <title> GoToMy PC </title> <head> <link rel="stylesheet" ...

Issues with CSS Styling not being applied properly on mobile devices in a React App deployed on Heroku

The Dilemma My React app is deployed on Heroku using create-react-app for bundling. The backend is a Node.js written in Typescript with node version 10.15.3. Locally, when I run the site using npm start, everything works perfectly. However, when I view t ...

Retrieve every potential option of a dropdown menu

One of my tasks involves working with a select field that has various values: africa europe america asia oceania Whenever a value is selected, I aim to apply a specific CSS class to a particular div, with the name of the CSS class matching the selected v ...

Is it possible to replace JavaScript files that are included in my index page if I am utilizing conditional comments specifically for IE9?

My website works perfectly in all browsers, except for IE9. After some investigation, I discovered that the issue lies with a jQuery plugin called multilevelpush.js. While it works great on other browsers, it simply won't cooperate with IE9. Upon fur ...

Is there a way to stop myself from accidentally clicking twice on the same tile?

I'm currently working on a game and facing an issue where my "X" gets deleted when clicking twice on the same tile. I am able to move my "X" around, but the double-click deletion is causing trouble. I attempted using booleans but struggle with them. I ...

Triggering animation with jQuery and CSS

I am working on a one-page site and I want to make a parallelogram disappear when a button is clicked for the next part. Currently, it is disappearing from right to left but I would like to change the direction to left to right. Additionally, I have a simi ...

The parameters sent through Ajax are coming back as undefined

I'm encountering an issue where all the data passed in my Ajax call to a PHP function is showing up as "UNDEFINED" on the server side. The JQuery debugged fine, so it seems like the problem lies within the PHP side of things. $('.js-checkout-shi ...

jQuery regex failing to display latest changes

I am running into some issues with my custom jQuery snippet that is supposed to dynamically display the results. Each H2 tag contains an image, and I want to ensure the image is positioned correctly next to the word "test." Here is the script in question: ...

Transforming JavaScript array UNIX timestamps into JavaScript Date objects

Is there a way to convert UNIX epoch integers into JavaScript Date objects using jQuery? Attempting to pass a large JSON array generated by PHP to Highmaps.JS, which works almost great. However, Highmaps expects a Date object and Date objects aren't ...

The CSS :active selector glutton

Within a <div> element, I have an <img>. I would like the entire div to scale down by 10% (using transform) when clicked on. While I have achieved this effect, there is one minor issue: clicking on the image itself does not trigger the scaling, ...

What is the most effective way to transfer a value from the server to a React user interface and incorporate it into the styling of the user interface?

Currently, I am utilizing Python as the backend for my React frontend. The backend is passing in values such as 17 or 87, and I aim to use these values as a percentage on the scale that I am constructing. Currently, the scale starts in the center: https:/ ...

Unexpected failure of Ajax response following a successful upload

I am currently using jQuery to upload files or images to a CodeIgniter controller. Despite facing some challenges, I have successfully managed to upload the image. However, I am encountering some strange behavior from the AJAX function. Issue: The AJAX ca ...

What is the best way to arrange card-columns in a horizontal order?

My smart-scrolling list of cards uses the card-columns style, but I find it frustrating that they are ordered top to bottom like this: 1 4 7 2 5 8 3 6 9 This vertical ordering becomes impractical when dealing with a large number of items. When I have 50 ...

Steps for appending a string to a variable

Currently working on creating a price configurator for a new lighting system within homes using Angular 7. Instead of using TypeScript and sass, I'm coding it in plain JavaScript. Page 1: The user will choose between a new building or an existing one ...

Tips on dynamically changing the position of a div: Utilize absolute positioning within a for loop

Is there a way to extract the position x value of my div and store it in a variable? How do we then iterate over it within a for loop? <div id="object"></div> <style> #object{ position:absolute; width:10px; height:10px; ...