Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing style of a button on click

Is it possible to change background-color of my button onClick function?

ex. click background-color: black, another click background-color: white

I've tried something like this.style, no result.

I've managed to get overlay working and insert needed data inside of it. But didn't managed to find any post that could help me. I am using react-bootstrap. This is my code.

  const metaDataOverlay = (   <div>   <style type="text/css">{`   .btn-overlay {     background-color: white;     margin-right: -15px;     margin-left: -15px;     padding-bottom: -20px;     padding: 0;   }   `}</style>        <ButtonToolbar>         <ButtonGroup>         <OverlayTrigger trigger={['hover', 'focus']} placement="left" overlay={popoverHoverFocus}>           <Button bsStyle="overlay" onClick={ clicked } onKeyPress={ keypress }>             <div className={bemBlocks.item().mix(bemBlocks.container("item"))} data-qa="hit">               <a href={url} onClick={(e)=>{e.preventDefault(); console.log("123")}}>                 <div>                   <img data-qa="poster" className={bemBlocks.item("poster")} src={result._source.poster} width="240" height="240"/>                 </div>               </a>             </div>           </Button>         </OverlayTrigger>         </ButtonGroup>       </ButtonToolbar>      </div>   ) 
like image 992
user3350597 Avatar asked Feb 01 '17 11:02

user3350597


People also ask

How do you style a click button?

Unfortunately, there is no :click pseudo selector. If you want to change styling on click, you should use Jquery/Javascript. It certainly is better than the "hack" for pure HTML / CSS.

How do I change the style of a button in HTML?

Use the :hover selector to change the style of a button when you move the mouse over it.

How do I change the text style of a button?

To select multiple button objects, click the first object, then press and hold the CTRL key while you click the other objects. Click the Style ribbon. Use the controls in the Text Style group to change the text style of a button object as follows: Click this to select a text style to apply to the objects.


2 Answers

You can try to use state to store the color. Maybe this would give you the idea how to solve the problem :

class Test extends React.Component {     constructor(){         super();          this.state = {            black: true         }     }      changeColor(){        this.setState({black: !this.state.black})     }      render(){         let btn_class = this.state.black ? "blackButton" : "whiteButton";          return (              <button className={btn_class} onClick={this.changeColor.bind(this)}>                   Button              </button>         )     } }  React.render(<Test />, document.getElementById('container')); 

Here is a fiddle.

like image 75
Boky Avatar answered Sep 19 '22 18:09

Boky


You also have access to event and current target of the event

handleClick = (event) => {    // accessible    event.target.style    event.target.classList //to change style via css } 
like image 27
Dmitriy Kovalenko Avatar answered Sep 20 '22 18:09

Dmitriy Kovalenko