How to add active class to a list of buttons onClick, they are not controlled by parentReact Js conditionally applying class attributesreact change the class of list item on clickSet focus on a react-bootstrap.Input field using refs.x.getDOMNode.focusHow to conditionally add attributes to React components?Passing data to parent in ReactHow do I reset class names on all JSX elements in a component on props received?setState changes state but doesn't re-renderupdate state child from parent's onClick on reactjsCan I connect just the onClick function to the Redux store, instead of connecting the button component?empty property and value in React fetch post requestReact Ref not getting text input field data in formReact Checkbox Depends on Parent State
What prevents ads from reading my password as I type it?
Did WWII Japanese soldiers engage in cannibalism of their enemies?
Pronouns when writing from the point of view of a robot
What does the ISO setting for mechanical 35mm film cameras actually do?
Repeated! Factorials!
Getting matrices labels
I was contacted by a private bank overseas to get my inheritance
Is space radiation a risk for space film photography, and how is this prevented?
Why am I not getting stuck in the loop
If the interviewer says "We have other interviews to conduct and then back to you in few days", is it a bad sign to not get the job?
What could prevent players from leaving an island?
What is the German idiom or expression for when someone is being hypocritical against their own teachings?
Did Captain America make out with his niece?
How to approach protecting my code as a research assistant? Should I be worried in the first place?
Changing Row Keys into Normal Rows
Is it double speak?
Is there a way to upload multiple discount counts into CiviDiscount?
Do you like Music? This word is Historic
Delete a folder with a lot of files
Why do proponents of guns oppose gun competency tests?
Our group keeps dying during the Lost Mine of Phandelver campaign. What are we doing wrong?
Non-small objects in categories
Did Apollo leave poop on the moon?
Does this smartphone photo show Mars just below the Sun?
How to add active class to a list of buttons onClick, they are not controlled by parent
React Js conditionally applying class attributesreact change the class of list item on clickSet focus on a react-bootstrap.Input field using refs.x.getDOMNode.focusHow to conditionally add attributes to React components?Passing data to parent in ReactHow do I reset class names on all JSX elements in a component on props received?setState changes state but doesn't re-renderupdate state child from parent's onClick on reactjsCan I connect just the onClick function to the Redux store, instead of connecting the button component?empty property and value in React fetch post requestReact Ref not getting text input field data in formReact Checkbox Depends on Parent State
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am trying to create a chatbox component like the facebook-status post. There are buttons on the bottom. When a user clicks them I load various box styles. I need to set the class of current button "active".
My component is not controlled by a parent so I cannot set any prop bases solution. I know how to implement a ternary operator but don't know how can I use it here?
I have tried these solutions but these all are parent based.
React Js conditionally applying class attributes
react change the class of list item on click
import React from "react"
import StyleButton from "./StyleButton"
class Post extends React.Component
constructor()
super()
this.state =
selectedClass: 'red',
displayBtns: true,
buttons: [1,2,3,4,5,6]
handleDisplayBtnClick = () =>
this.setState(prevState => (
displayBtns: !prevState.displayBtns
))
handleChange = e =>
let btnId = e.currentTarget.dataset.id;
switch (btnId)
case '1':
this.setState(selectedClass: "purple" )
break;
case '2':
this.setState(selectedClass: "cyan" )
break;
case '3':
this.setState(selectedClass: "ballons" )
break;
case '4':
this.setState(selectedClass: "clouds" )
break;
case '5':
this.setState(selectedClass: "thumbs" )
break;
case '6':
this.setState(selectedClass: "smile" )
break;
default:
this.setState(selectedClass: "default" )
render()
return(
<div className=`bg container $this.state.selectedClass` >
<input className="input" placeholder="What's on your mind?" type="text"/>
<button className=this.state.displayBtns ? 'closeBtn' : 'openBtn' onClick=this.handleDisplayBtnClick> </button>
this.state.displayBtns ?
<div className="btns" >
this.state.buttons.map(eachButtonNumber =>
<button
key=eachButtonNumber
data-id=eachButtonNumber
onClick=this.handleChange
> </button>
)
</div>
: null
</div>
)
export default Post
I want to add an "active" class when I click it on the buttons which are rendered from "buttons.map". Right now my buttons is changing the Div class to change the background.
<button
key=eachButtonNumber
data-id=eachButtonNumber
onClick=this.handleChange > </button>
Also, want to know If this is the right way of doing it?
Posted the whole code and GIF at "https://github.com/ankursehdev/facebook-post-box-reactjs"
My Rendered HTML - I want "active" on the list of buttons.
<div class="parent">
<div class="bg container smile">
<input class="input" placeholder="What's on your mind?" type="text">
<button class="closeBtn"> </button>
<div class="btns">
<button data-id="1"> </button>
<button data-id="2"> </button>
<button data-id="3"> </button>
<button data-id="4"> </button>
<button data-id="5"> </button>
<button data-id="6"> </button>
</div>
</div>
</div>
reactjs
add a comment |
I am trying to create a chatbox component like the facebook-status post. There are buttons on the bottom. When a user clicks them I load various box styles. I need to set the class of current button "active".
My component is not controlled by a parent so I cannot set any prop bases solution. I know how to implement a ternary operator but don't know how can I use it here?
I have tried these solutions but these all are parent based.
React Js conditionally applying class attributes
react change the class of list item on click
import React from "react"
import StyleButton from "./StyleButton"
class Post extends React.Component
constructor()
super()
this.state =
selectedClass: 'red',
displayBtns: true,
buttons: [1,2,3,4,5,6]
handleDisplayBtnClick = () =>
this.setState(prevState => (
displayBtns: !prevState.displayBtns
))
handleChange = e =>
let btnId = e.currentTarget.dataset.id;
switch (btnId)
case '1':
this.setState(selectedClass: "purple" )
break;
case '2':
this.setState(selectedClass: "cyan" )
break;
case '3':
this.setState(selectedClass: "ballons" )
break;
case '4':
this.setState(selectedClass: "clouds" )
break;
case '5':
this.setState(selectedClass: "thumbs" )
break;
case '6':
this.setState(selectedClass: "smile" )
break;
default:
this.setState(selectedClass: "default" )
render()
return(
<div className=`bg container $this.state.selectedClass` >
<input className="input" placeholder="What's on your mind?" type="text"/>
<button className=this.state.displayBtns ? 'closeBtn' : 'openBtn' onClick=this.handleDisplayBtnClick> </button>
this.state.displayBtns ?
<div className="btns" >
this.state.buttons.map(eachButtonNumber =>
<button
key=eachButtonNumber
data-id=eachButtonNumber
onClick=this.handleChange
> </button>
)
</div>
: null
</div>
)
export default Post
I want to add an "active" class when I click it on the buttons which are rendered from "buttons.map". Right now my buttons is changing the Div class to change the background.
<button
key=eachButtonNumber
data-id=eachButtonNumber
onClick=this.handleChange > </button>
Also, want to know If this is the right way of doing it?
Posted the whole code and GIF at "https://github.com/ankursehdev/facebook-post-box-reactjs"
My Rendered HTML - I want "active" on the list of buttons.
<div class="parent">
<div class="bg container smile">
<input class="input" placeholder="What's on your mind?" type="text">
<button class="closeBtn"> </button>
<div class="btns">
<button data-id="1"> </button>
<button data-id="2"> </button>
<button data-id="3"> </button>
<button data-id="4"> </button>
<button data-id="5"> </button>
<button data-id="6"> </button>
</div>
</div>
</div>
reactjs
add a comment |
I am trying to create a chatbox component like the facebook-status post. There are buttons on the bottom. When a user clicks them I load various box styles. I need to set the class of current button "active".
My component is not controlled by a parent so I cannot set any prop bases solution. I know how to implement a ternary operator but don't know how can I use it here?
I have tried these solutions but these all are parent based.
React Js conditionally applying class attributes
react change the class of list item on click
import React from "react"
import StyleButton from "./StyleButton"
class Post extends React.Component
constructor()
super()
this.state =
selectedClass: 'red',
displayBtns: true,
buttons: [1,2,3,4,5,6]
handleDisplayBtnClick = () =>
this.setState(prevState => (
displayBtns: !prevState.displayBtns
))
handleChange = e =>
let btnId = e.currentTarget.dataset.id;
switch (btnId)
case '1':
this.setState(selectedClass: "purple" )
break;
case '2':
this.setState(selectedClass: "cyan" )
break;
case '3':
this.setState(selectedClass: "ballons" )
break;
case '4':
this.setState(selectedClass: "clouds" )
break;
case '5':
this.setState(selectedClass: "thumbs" )
break;
case '6':
this.setState(selectedClass: "smile" )
break;
default:
this.setState(selectedClass: "default" )
render()
return(
<div className=`bg container $this.state.selectedClass` >
<input className="input" placeholder="What's on your mind?" type="text"/>
<button className=this.state.displayBtns ? 'closeBtn' : 'openBtn' onClick=this.handleDisplayBtnClick> </button>
this.state.displayBtns ?
<div className="btns" >
this.state.buttons.map(eachButtonNumber =>
<button
key=eachButtonNumber
data-id=eachButtonNumber
onClick=this.handleChange
> </button>
)
</div>
: null
</div>
)
export default Post
I want to add an "active" class when I click it on the buttons which are rendered from "buttons.map". Right now my buttons is changing the Div class to change the background.
<button
key=eachButtonNumber
data-id=eachButtonNumber
onClick=this.handleChange > </button>
Also, want to know If this is the right way of doing it?
Posted the whole code and GIF at "https://github.com/ankursehdev/facebook-post-box-reactjs"
My Rendered HTML - I want "active" on the list of buttons.
<div class="parent">
<div class="bg container smile">
<input class="input" placeholder="What's on your mind?" type="text">
<button class="closeBtn"> </button>
<div class="btns">
<button data-id="1"> </button>
<button data-id="2"> </button>
<button data-id="3"> </button>
<button data-id="4"> </button>
<button data-id="5"> </button>
<button data-id="6"> </button>
</div>
</div>
</div>
reactjs
I am trying to create a chatbox component like the facebook-status post. There are buttons on the bottom. When a user clicks them I load various box styles. I need to set the class of current button "active".
My component is not controlled by a parent so I cannot set any prop bases solution. I know how to implement a ternary operator but don't know how can I use it here?
I have tried these solutions but these all are parent based.
React Js conditionally applying class attributes
react change the class of list item on click
import React from "react"
import StyleButton from "./StyleButton"
class Post extends React.Component
constructor()
super()
this.state =
selectedClass: 'red',
displayBtns: true,
buttons: [1,2,3,4,5,6]
handleDisplayBtnClick = () =>
this.setState(prevState => (
displayBtns: !prevState.displayBtns
))
handleChange = e =>
let btnId = e.currentTarget.dataset.id;
switch (btnId)
case '1':
this.setState(selectedClass: "purple" )
break;
case '2':
this.setState(selectedClass: "cyan" )
break;
case '3':
this.setState(selectedClass: "ballons" )
break;
case '4':
this.setState(selectedClass: "clouds" )
break;
case '5':
this.setState(selectedClass: "thumbs" )
break;
case '6':
this.setState(selectedClass: "smile" )
break;
default:
this.setState(selectedClass: "default" )
render()
return(
<div className=`bg container $this.state.selectedClass` >
<input className="input" placeholder="What's on your mind?" type="text"/>
<button className=this.state.displayBtns ? 'closeBtn' : 'openBtn' onClick=this.handleDisplayBtnClick> </button>
this.state.displayBtns ?
<div className="btns" >
this.state.buttons.map(eachButtonNumber =>
<button
key=eachButtonNumber
data-id=eachButtonNumber
onClick=this.handleChange
> </button>
)
</div>
: null
</div>
)
export default Post
I want to add an "active" class when I click it on the buttons which are rendered from "buttons.map". Right now my buttons is changing the Div class to change the background.
<button
key=eachButtonNumber
data-id=eachButtonNumber
onClick=this.handleChange > </button>
Also, want to know If this is the right way of doing it?
Posted the whole code and GIF at "https://github.com/ankursehdev/facebook-post-box-reactjs"
My Rendered HTML - I want "active" on the list of buttons.
<div class="parent">
<div class="bg container smile">
<input class="input" placeholder="What's on your mind?" type="text">
<button class="closeBtn"> </button>
<div class="btns">
<button data-id="1"> </button>
<button data-id="2"> </button>
<button data-id="3"> </button>
<button data-id="4"> </button>
<button data-id="5"> </button>
<button data-id="6"> </button>
</div>
</div>
</div>
reactjs
reactjs
edited Mar 27 at 4:31
Ankur Sehdev
asked Mar 27 at 4:08
Ankur SehdevAnkur Sehdev
411 silver badge5 bronze badges
411 silver badge5 bronze badges
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
If your buttons have a little more detail to them this is pretty easy. If instead of [1,2,3,4,5,6]
for your buttons, if the array had an object for each button, each button could have properties that can be matched to the state. So maybe like this:
[
id: 1, buttonName: 'purple',
id: 2, buttonName: 'cyan',
id: 3, buttonName: 'ballons',
id: 4, buttonName: 'clouds',
id: 5, buttonName: 'thumbs',
id: 6, buttonName: 'smile',
]
Then in your map
loop, each button is checking if the selectedClass
state matches its buttonName
property and adding an active
class in that case. Note you will need to point to id
property now for the numeric id of each button.
this.state.buttons.map(eachButtonNumber =>
<button
className=this.state.selectedClass === eachButtonNumber.buttonName && 'active'
key=eachButtonNumber.id
data-id=eachButtonNumber.id
onClick=this.handleChange
>
</button>
)
Yes, I have full control over that array. And do you think this is the right approach to write React app in general? I mean the full application. and Thanks
– Ankur Sehdev
Mar 27 at 14:09
It's the most React-y way to manage conditional classes in a loop that I know of. Happy to help!
– BugsArePeopleToo
Mar 27 at 14:13
I am getting a warning after using this: So I am using the ternary operator. Following is the warning. > Warning: Receivedfalse
for a non-boolean attributeclassName
. If you want to write it to the DOM, pass a string instead: className="false" or className=value.toString(). If you used to conditionally omit it with className=condition && value, pass className=condition ? value : undefined instead.
– Ankur Sehdev
Mar 27 at 14:21
add a comment |
Simplest way to do this is to get the className to be applied inside the render before returning the jsx.
Like in render you can write the condition as below
let classNameStyle = ""
if(this.state.displayBtns)
classNameStyle = 'closeBtn '
else
classNameStyle = 'openBtn '
if(this.state.active)
classNameStyle = classNameStyle + 'active'
Now apply this classNameStyle value to className property of button.
(Note ) See the space in assignment of first className
Hello Raj, I am trying to apply active class to ``` <button key=eachButtonNumber data-id=eachButtonNumber onClick=this.handleChange > </button> not the openBtn and CloseBtn. There is a list of buttons from the buttons.map.
– Ankur Sehdev
Mar 27 at 4:26
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55369669%2fhow-to-add-active-class-to-a-list-of-buttons-onclick-they-are-not-controlled-by%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
If your buttons have a little more detail to them this is pretty easy. If instead of [1,2,3,4,5,6]
for your buttons, if the array had an object for each button, each button could have properties that can be matched to the state. So maybe like this:
[
id: 1, buttonName: 'purple',
id: 2, buttonName: 'cyan',
id: 3, buttonName: 'ballons',
id: 4, buttonName: 'clouds',
id: 5, buttonName: 'thumbs',
id: 6, buttonName: 'smile',
]
Then in your map
loop, each button is checking if the selectedClass
state matches its buttonName
property and adding an active
class in that case. Note you will need to point to id
property now for the numeric id of each button.
this.state.buttons.map(eachButtonNumber =>
<button
className=this.state.selectedClass === eachButtonNumber.buttonName && 'active'
key=eachButtonNumber.id
data-id=eachButtonNumber.id
onClick=this.handleChange
>
</button>
)
Yes, I have full control over that array. And do you think this is the right approach to write React app in general? I mean the full application. and Thanks
– Ankur Sehdev
Mar 27 at 14:09
It's the most React-y way to manage conditional classes in a loop that I know of. Happy to help!
– BugsArePeopleToo
Mar 27 at 14:13
I am getting a warning after using this: So I am using the ternary operator. Following is the warning. > Warning: Receivedfalse
for a non-boolean attributeclassName
. If you want to write it to the DOM, pass a string instead: className="false" or className=value.toString(). If you used to conditionally omit it with className=condition && value, pass className=condition ? value : undefined instead.
– Ankur Sehdev
Mar 27 at 14:21
add a comment |
If your buttons have a little more detail to them this is pretty easy. If instead of [1,2,3,4,5,6]
for your buttons, if the array had an object for each button, each button could have properties that can be matched to the state. So maybe like this:
[
id: 1, buttonName: 'purple',
id: 2, buttonName: 'cyan',
id: 3, buttonName: 'ballons',
id: 4, buttonName: 'clouds',
id: 5, buttonName: 'thumbs',
id: 6, buttonName: 'smile',
]
Then in your map
loop, each button is checking if the selectedClass
state matches its buttonName
property and adding an active
class in that case. Note you will need to point to id
property now for the numeric id of each button.
this.state.buttons.map(eachButtonNumber =>
<button
className=this.state.selectedClass === eachButtonNumber.buttonName && 'active'
key=eachButtonNumber.id
data-id=eachButtonNumber.id
onClick=this.handleChange
>
</button>
)
Yes, I have full control over that array. And do you think this is the right approach to write React app in general? I mean the full application. and Thanks
– Ankur Sehdev
Mar 27 at 14:09
It's the most React-y way to manage conditional classes in a loop that I know of. Happy to help!
– BugsArePeopleToo
Mar 27 at 14:13
I am getting a warning after using this: So I am using the ternary operator. Following is the warning. > Warning: Receivedfalse
for a non-boolean attributeclassName
. If you want to write it to the DOM, pass a string instead: className="false" or className=value.toString(). If you used to conditionally omit it with className=condition && value, pass className=condition ? value : undefined instead.
– Ankur Sehdev
Mar 27 at 14:21
add a comment |
If your buttons have a little more detail to them this is pretty easy. If instead of [1,2,3,4,5,6]
for your buttons, if the array had an object for each button, each button could have properties that can be matched to the state. So maybe like this:
[
id: 1, buttonName: 'purple',
id: 2, buttonName: 'cyan',
id: 3, buttonName: 'ballons',
id: 4, buttonName: 'clouds',
id: 5, buttonName: 'thumbs',
id: 6, buttonName: 'smile',
]
Then in your map
loop, each button is checking if the selectedClass
state matches its buttonName
property and adding an active
class in that case. Note you will need to point to id
property now for the numeric id of each button.
this.state.buttons.map(eachButtonNumber =>
<button
className=this.state.selectedClass === eachButtonNumber.buttonName && 'active'
key=eachButtonNumber.id
data-id=eachButtonNumber.id
onClick=this.handleChange
>
</button>
)
If your buttons have a little more detail to them this is pretty easy. If instead of [1,2,3,4,5,6]
for your buttons, if the array had an object for each button, each button could have properties that can be matched to the state. So maybe like this:
[
id: 1, buttonName: 'purple',
id: 2, buttonName: 'cyan',
id: 3, buttonName: 'ballons',
id: 4, buttonName: 'clouds',
id: 5, buttonName: 'thumbs',
id: 6, buttonName: 'smile',
]
Then in your map
loop, each button is checking if the selectedClass
state matches its buttonName
property and adding an active
class in that case. Note you will need to point to id
property now for the numeric id of each button.
this.state.buttons.map(eachButtonNumber =>
<button
className=this.state.selectedClass === eachButtonNumber.buttonName && 'active'
key=eachButtonNumber.id
data-id=eachButtonNumber.id
onClick=this.handleChange
>
</button>
)
answered Mar 27 at 4:48
BugsArePeopleTooBugsArePeopleToo
2,0021 gold badge9 silver badges12 bronze badges
2,0021 gold badge9 silver badges12 bronze badges
Yes, I have full control over that array. And do you think this is the right approach to write React app in general? I mean the full application. and Thanks
– Ankur Sehdev
Mar 27 at 14:09
It's the most React-y way to manage conditional classes in a loop that I know of. Happy to help!
– BugsArePeopleToo
Mar 27 at 14:13
I am getting a warning after using this: So I am using the ternary operator. Following is the warning. > Warning: Receivedfalse
for a non-boolean attributeclassName
. If you want to write it to the DOM, pass a string instead: className="false" or className=value.toString(). If you used to conditionally omit it with className=condition && value, pass className=condition ? value : undefined instead.
– Ankur Sehdev
Mar 27 at 14:21
add a comment |
Yes, I have full control over that array. And do you think this is the right approach to write React app in general? I mean the full application. and Thanks
– Ankur Sehdev
Mar 27 at 14:09
It's the most React-y way to manage conditional classes in a loop that I know of. Happy to help!
– BugsArePeopleToo
Mar 27 at 14:13
I am getting a warning after using this: So I am using the ternary operator. Following is the warning. > Warning: Receivedfalse
for a non-boolean attributeclassName
. If you want to write it to the DOM, pass a string instead: className="false" or className=value.toString(). If you used to conditionally omit it with className=condition && value, pass className=condition ? value : undefined instead.
– Ankur Sehdev
Mar 27 at 14:21
Yes, I have full control over that array. And do you think this is the right approach to write React app in general? I mean the full application. and Thanks
– Ankur Sehdev
Mar 27 at 14:09
Yes, I have full control over that array. And do you think this is the right approach to write React app in general? I mean the full application. and Thanks
– Ankur Sehdev
Mar 27 at 14:09
It's the most React-y way to manage conditional classes in a loop that I know of. Happy to help!
– BugsArePeopleToo
Mar 27 at 14:13
It's the most React-y way to manage conditional classes in a loop that I know of. Happy to help!
– BugsArePeopleToo
Mar 27 at 14:13
I am getting a warning after using this: So I am using the ternary operator. Following is the warning. > Warning: Received
false
for a non-boolean attribute className
. If you want to write it to the DOM, pass a string instead: className="false" or className=value.toString(). If you used to conditionally omit it with className=condition && value, pass className=condition ? value : undefined instead.– Ankur Sehdev
Mar 27 at 14:21
I am getting a warning after using this: So I am using the ternary operator. Following is the warning. > Warning: Received
false
for a non-boolean attribute className
. If you want to write it to the DOM, pass a string instead: className="false" or className=value.toString(). If you used to conditionally omit it with className=condition && value, pass className=condition ? value : undefined instead.– Ankur Sehdev
Mar 27 at 14:21
add a comment |
Simplest way to do this is to get the className to be applied inside the render before returning the jsx.
Like in render you can write the condition as below
let classNameStyle = ""
if(this.state.displayBtns)
classNameStyle = 'closeBtn '
else
classNameStyle = 'openBtn '
if(this.state.active)
classNameStyle = classNameStyle + 'active'
Now apply this classNameStyle value to className property of button.
(Note ) See the space in assignment of first className
Hello Raj, I am trying to apply active class to ``` <button key=eachButtonNumber data-id=eachButtonNumber onClick=this.handleChange > </button> not the openBtn and CloseBtn. There is a list of buttons from the buttons.map.
– Ankur Sehdev
Mar 27 at 4:26
add a comment |
Simplest way to do this is to get the className to be applied inside the render before returning the jsx.
Like in render you can write the condition as below
let classNameStyle = ""
if(this.state.displayBtns)
classNameStyle = 'closeBtn '
else
classNameStyle = 'openBtn '
if(this.state.active)
classNameStyle = classNameStyle + 'active'
Now apply this classNameStyle value to className property of button.
(Note ) See the space in assignment of first className
Hello Raj, I am trying to apply active class to ``` <button key=eachButtonNumber data-id=eachButtonNumber onClick=this.handleChange > </button> not the openBtn and CloseBtn. There is a list of buttons from the buttons.map.
– Ankur Sehdev
Mar 27 at 4:26
add a comment |
Simplest way to do this is to get the className to be applied inside the render before returning the jsx.
Like in render you can write the condition as below
let classNameStyle = ""
if(this.state.displayBtns)
classNameStyle = 'closeBtn '
else
classNameStyle = 'openBtn '
if(this.state.active)
classNameStyle = classNameStyle + 'active'
Now apply this classNameStyle value to className property of button.
(Note ) See the space in assignment of first className
Simplest way to do this is to get the className to be applied inside the render before returning the jsx.
Like in render you can write the condition as below
let classNameStyle = ""
if(this.state.displayBtns)
classNameStyle = 'closeBtn '
else
classNameStyle = 'openBtn '
if(this.state.active)
classNameStyle = classNameStyle + 'active'
Now apply this classNameStyle value to className property of button.
(Note ) See the space in assignment of first className
answered Mar 27 at 4:18
Raj SaraogiRaj Saraogi
2672 silver badges11 bronze badges
2672 silver badges11 bronze badges
Hello Raj, I am trying to apply active class to ``` <button key=eachButtonNumber data-id=eachButtonNumber onClick=this.handleChange > </button> not the openBtn and CloseBtn. There is a list of buttons from the buttons.map.
– Ankur Sehdev
Mar 27 at 4:26
add a comment |
Hello Raj, I am trying to apply active class to ``` <button key=eachButtonNumber data-id=eachButtonNumber onClick=this.handleChange > </button> not the openBtn and CloseBtn. There is a list of buttons from the buttons.map.
– Ankur Sehdev
Mar 27 at 4:26
Hello Raj, I am trying to apply active class to ``` <button key=eachButtonNumber data-id=eachButtonNumber onClick=this.handleChange > </button> not the openBtn and CloseBtn. There is a list of buttons from the buttons.map.
– Ankur Sehdev
Mar 27 at 4:26
Hello Raj, I am trying to apply active class to ``` <button key=eachButtonNumber data-id=eachButtonNumber onClick=this.handleChange > </button> not the openBtn and CloseBtn. There is a list of buttons from the buttons.map.
– Ankur Sehdev
Mar 27 at 4:26
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55369669%2fhow-to-add-active-class-to-a-list-of-buttons-onclick-they-are-not-controlled-by%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown