javascript - reverting the changes when multiple clicks on same div -
i have div mapped render number of times according data sent database. i'm setting background color in div when clicking on div. how can revert changes if click div again?
my code
handleclick(id) { let el = document.getelementbyid(id); if (el) { el.style.backgroundcolor = "#0b97c4"; el.style.color = "#ffffff"; this.setstate({ votecount: this.state.votecount + 1 }) } this.props.submitvote(vote_object) } render() { let {contents, submitvote, postid, voted_id} = this.props return ( <div classname="txt_vote_bar_div" id={this.props.contents.post_poll_content_id}> <p classname="txt_vote_choice" id={this.props.contents.post_poll_content_id} onclick={() => {this.handleclick(this.props.contents.post_poll_content_id);}}> {contents.content} </p> <p classname="txt_tot_votes"> {this.props.contents.votes_percentage}% ({this.state.votecount} votes) </p> </div> ); };
using above code i'm changing background of div successfully. how can remove background color if select same div again (remove background color or change color)
i make txt_vote_choice
"old" css name, , txt_vote_clicked
new classname
want change. onclick
, toggle classname
between these 2 classes trick.
handleclick(id) { let el = document.getelementbyid(id); if (el) { if el.class == "txt_vote_choice" ? "txt_vote_clicked": "txt_vote_choice"; this.setstate({ votecount: this.state.votecount + 1 }) } this.props.submitvote(vote_object) } render() { let {contents, submitvote, postid, voted_id, voted_css} = this.props return ( <div classname="txt_vote_bar_div" id={this.props.contents.post_poll_content_id}> <p classname={voted_css} id={this.props.contents.post_poll_content_id} onclick={() => {this.handleclick(this.props.contents.post_poll_content_id);}}> {contents.content} </p> <p classname="txt_tot_votes"> {this.props.contents.votes_percentage}%({this.state.votecount} votes) </p> </div> ); };
then update css accordingly
Comments
Post a Comment