Adjust the color of the text based on a conditional in react

I am looking for a way to select elements and change text color in my unordered list based on specific conditions. Below is an example of the structure I have:

<div style={styles.passwordRules}>
  <ul style={styles.listOne}>
    <li style={styles.li}><span style={styles.error} id="test">8 to 16 characters</span></li>
    <li style={styles.li}><span style={styles.fontNormal}>Uppercase character</span></li>
  </ul>
  <ul style={styles.listTwo}>
    <li style={styles.li}><span style={styles.fontNormal}>A number</span></li>
    <li style={styles.li}><span style={styles.fontNormal}>Lowercase character</span></li>
  </ul>
  <ul style={styles.listThree}>
    <li style={styles.li}><span style={styles.fontLink}>No personal information</span></li>
  </ul>
</div>

I would like to apply different text colors based on conditions, similar to the following example:

if(text === 'foo') {
//change color to gray
}

Please keep in mind that I am unable to use getElementById

Answer №1

Have you considered implementing it in the following way?

<li className={text === 'foo' ? styles.class1 : styles.class2 } .../>

Answer №2

Expanding on Carl Edwards's response, if your main goal is to modify the color, you can achieve that with the following code snippet:

<li style={{color: text === "foo" ? "trueColor" : "falseColor"}} ... />

Answer №3

One way to achieve this is by implementing the following:

<li style={text === 'bar' ? _.extend(styles.style, styles.styleBar) : styles.style }>

Alternatively, if you're working with ES6:

<li style={text === 'barr' ? Object.assign({}, styles.style, styles.styleBar) : styles.style }>

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

Why won't AngularJS ng-click function properly?

I'm attempting to implement form validation using JavaScript. However, when I include the following line document.getElementById("one").setAttribute("ng-click", "insertData()"); inside the validateForm function, it doesn't work properly after ...

What is the best way to distinguish between server and client folders during committing?

I'm seeking assistance on how to properly commit the server and client folders for my new project. I am working on setting up a back-end using node-js and front-end with react-js. I have setup two separate folders, "client" for react and "server" for ...

The Forge Viewer's getState() function is providing inaccurate values for individual items

In our Angular application, we have integrated the latest version of Forge Viewer and are storing the current state of the viewer in our database for future restoration. After thorough testing, we discovered that isolated nodes are not being saved correct ...

What is the best way to detect and handle the destroy event in Kendo UI grids when a click event

How can I trigger a function when the delete button in a grid is clicked using JavaScript? ...

Ways to conceal numerous objects

I need to create a function where I can hide multiple elements by pressing a button based on the value of checkboxes. If a checkbox is checked, the corresponding element should be hidden. Currently, I have some code that only works for the first element: ...

Populate an array using a web API AJAX request in jQuery

I'm encountering an issue with jQuery or Javascript. My goal is to display additional flags in Google maps from an IP array. I've successfully passed the IP array to the function, but when I use ajax to call the web API multiple times correspondi ...

Tips on selecting specific data from a nested JSON array based on conditions and fetching just one value from the initial filtered result with Javascript (designed for Google Sheets)

Utilizing the TMDB API for retrieving movie data and integrating it into a Google Sheet. The original Google Sheet was revamped from Reddit user 6745408's "MediaSheet 3.0". This sheet incorporates a Javascript-based script. By following the patterns/c ...

Invoking a static method within a cshtml document

I am currently working on implementing a clickable DIV within a vertical tab panel. My goal is to have a specific static method called when the DIV is clicked. Here is what I have done: <div class="tabbable tabs-left"> <ul class="nav nav-tabs"> ...

Refresh Calendar Element at the Stroke of Midnight

I have created a basic calendar component that I would like to discuss. This is how the component looks: https://i.sstatic.net/Slb2X.jpg Below is the code for this component: import React from "react"; import { View, Text, StyleSheet } from &qu ...

Tips for hiding one sub-navigation menu when hovering over another sub-navigation menu

Setting up the main navigation menu: <ul class="menu12"> <li><a href="/">Home</a></li> <li><a href="/">About</a> <ul> <li><a href="/">History</a></li> <li ...

Tips for responding to and disabling a specific button in Vuetify.js after clicking the follow or unfollow button

I have a situation where I need to implement a functionality for a series of buttons with follow and unfollow statuses. When a user clicks on any button, I want the status to change after a brief delay and deactivation, followed by reactivation. For instan ...

Customize the CSS for MaterialUI's TablePagination's MenuItem

Having trouble figuring out how to override CSS for the child component MenuItem within TablePagination? Check out this link for more information: https://github.com/mui-org/material-ui/blob/master/packages/material-ui/src/TablePagination/TablePagination.j ...

Troubleshooting issues with the add-comment and remove-comment functionalities in a react-redux application

I'm working on a comment widget using react-redux. I've encountered an issue where, when I click on the add comment button, nothing happens. Also, when I try to delete a comment, I get an error saying that map is not a function even though I am p ...

What are some ways to incorporate texture into objects using three.js?

Having trouble adding texture to my central sphere (earth) without the object disappearing. Can someone provide guidance on where I might be making a mistake? Your help is appreciated. For reference, here is the link to my jsbin http://jsbin.com/cabape/edi ...

Implementing Observable in NativeScript with TypeScript - A Step-by-Step Guide

Recently, I delved into the world of native-script framework and decided to dive into the "Get Started with JavaScript" tutorial provided on their official website. My background in Java has made me more comfortable with typescript, so I attempted to swap ...

What is the correct way to align labels to the right in a column layout?

I just started learning React JS and I'm using Material-UI. One issue I encountered is that when I use the column layout with an 'xs' value, the component inside the column appears aligned to the left. To align it to the right, I tried incr ...

Ember 2: Display a loading message only if the IDs were part of the initial response

I frequently use the following code snippet in my projects: {{#each model.posts as |post|}} <div>post.title</div> {{else}} <div>Loading the posts...</div> {{/each}} However, I sometimes face uncertainty regarding whether t ...

Click to toggle information using Jquery

I am attempting to create a button that, when clicked, will toggle between displaying temperature in Fahrenheit and Celsius. While I have been able to make it work initially, the toggle only occurs once and then stops. I have experimented with separate if ...

What are some ways to sort through JSON data efficiently?

I am in need of filtering JSON data based on specific parameters. When using the GET method at http://localhost:5000/api/car?bodyTypeId=2, I expect to receive only JSON objects with bodyTypeId equal to 2. However, all objects are being returned: [ { ...

What is the best method to include a label in a React phone input field?

Currently, I am integrating the react-phone-input-2 library into my project. For all other input fields, I am using Material UI's Textfield component with labels. However, I am wondering if it is possible to include a label for the react phone input s ...