I am trying to learn how to use forms in React.
It is a form with options to select.
On an onclick event, the clicked span tag should change the background color (just to show it was clicked), I am able to achieve this for a single tag but for multiple tags if I click on a single span bg color of both the tags gets changed.
Code:
import React from "react";
class MyForm extends React.Component {
constructor(props) {
super(props);
this.state = {
userClick: true
};
this.handleChange = this.handleChange.bind(this);
this.handleChange1 = this.handleChange1.bind(this);
}
handleChange(e) {
alert(`You selected... ${e.target.textContent}`);
this.setState({
userClick: !this.state.userClick
});
}
handleChange1(e) {
alert(`You selected... ${e.target.textContent}`);
this.setState({
userClick: !this.state.userClick
});
}
render() {
return (
<div className="form-mode">
<h6>Services:</h6>
<div className="spans">
<span
onClick={this.handleChange}
className={this.state.userClick ? "youClicked" : "unClicked"}
>
{" "}
inPerson{" "}
</span>
<span onClick={this.handleChange1} className={this.state.userClick ? "youClicked" : "unClicked"}> Virtual</span>
</div>
</div>
);
}
}
export default MyForm;