1

I work with buttons, and on click i need to change color button.

I have two button: favorite and stats, and onclick i need to change to custom color, for ex favorite is orange, stats blue.

How i can change color onclick?

<div className={classes.fotter_button}>
                    <button>
                      <FontAwesomeIcon
                        icon={faStar}
                        aria-hidden="true"
                      />
                    </button>
                    <button>
                      <FontAwesomeIcon
                        icon={faChartBar}
                        aria-hidden="true"
                      />
                    </button>
                  </div>
1
  • Please see stackoverflow.com/help/how-to-ask. You need to be more specific about what you're having trouble with, and you need to show some code related to what you're asking about. Commented May 25, 2021 at 16:04

2 Answers 2

2
const [favorite,setFavorite]=useState(false)
const [stats,setStats]=useState(false)

<div className={classes.fotter_button}>
                <button onClick={()=>{setFavorite(true)}} style={{backgroundColor:favorite==true?"orange":""}}>
                  <FontAwesomeIcon
                    icon={faStar}
                    aria-hidden="true"
                  />
                </button>
                <button onClick={()=>{setStats(true)}} style={{backgroundColor:stats==true?"blue":""}}>
                  <FontAwesomeIcon
                    icon={faChartBar}
                    aria-hidden="true"
                  />
                </button>
              </div>
Sign up to request clarification or add additional context in comments.

8 Comments

The same result all butons on the form change colors
What do you mean? I made a change to each button individually. Did you put in the code that way?
Yes i need to change individually each color of button
so, what the problem? this code change individually each color of button. Explain yourself
This code change color all butons which i have on the form
|
2

One way is to add state variable in your component, and use a function to change the state variable between two values (true, false). Apply the button styling based on the value of the state variable.

const MyToggleButton = () => {
 const [toggle, setToggle] = React.useState(false);
 const toggleButton = () => setToggle(!toggle);
 
 return (
  <>
    <button style={{backgroundColor: toggle ? '#FFF' : '#000'}} onClick={toggleButton}>Click Me</button>
  </>
 );
}

My example is pretty generic; if possible consider using a state variable that more aptly describes your buttons two states.

2 Comments

Onclick all buton which i have in the form change color
@Andrew You need a state variable per button, or (like my example) create a button component, which you reuse.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.