How do I target and change the style of a different element when I click a button in react

AnatuGreen

How can I target a different react element inside the same component when I click on a button and how can this work when I reuse the component several times in my App.js?

I have created a react component that has a div and a button. By default, I made the div hidden by creating a "hidden" CSS style and giving the div the class "hidden". Now what I want to achieve is that when I click the button, it will call a function that will make the div become visible by removing the hidden class from it's classList. My react component was created with class and I am binding the "this" keyword in constructor.

Now I have gone ahead to call the re-usable component above in four places in my App.js. On the first instance of the component, when I click the button, the div becomes visible as expected, but in other instances, when I click the buttons, the other corresponding divs do not become visible as expected per component instance. So my button click only affect the first instance of my react component while the remaining do not act accordingly.

This is the function I am calling by clicking the button

handleFinish() {
        console.log("CLickedddd");
        const editBtn = document.querySelectorAll(".ResetButton");
        for (let i = 0; i < editBtn.length; i++) {
          editBtn[i].classList.remove("hidden");
        }

This is the button and div:

<button id="finish" onClick={this.handleFinish}>
            Finish
          </button>
          <div style={{height: 300, width: 300, backgroundColor: "red"}} class="hidden">
            
          </div>
Apostolos

You should use a state variable to control your div's visibility.

class App extends Component {
  constructor() {
    super();
    this.state = { hidden: false };
  }

  handleFinish = () => {
    console.log("CLickedddd");
    this.setState({ hidden: !this.state.hidden });
  };

  render() {
    return (
      <>
        <button id="finish" onClick={this.handleFinish}>
          Finish
        </button>
        <div
          style={{ height: 300, width: 300, backgroundColor: "red" }}
          className={this.state.hidden ? "hidden" : ""}
        ></div>
      </>
    );
  }
}

See this sandbox

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I change the active class to the LI element when I click next/previous button?

How to change style of a different component on button click in react?

Why does my button's style change when I click on it?

How to change the caret when i click the button?

How can I create a HTML/JavaScript element that when I click it it shows a different element in React.js?

How do I style a select element in React?

react native: How can I change a view when i click a button from another page?

How do I change a button's color when pressed and reset to original color when a different button are pressed?

How do I change background when click on it?

Using jQuery, how do I change the input element's value when I click outside of the input box?

how do i change an image if i click on a button in Swift?

How do I change text every time I click the button?

How can I change the listModel of an listview when I click on a button?

How Do I Target Only One Button To Change Text When Clicked?

How to change text and button image (or button) when I click

How do I get the id of div wrapper in React when childnodes are the click event target?

How to highlight an element when I click on a different element in angular

How do I solve this problem where I get 'unable to locate the element' error when trying to click on a button that expands on click

How do I target a span within a label element when a checkbox is checked using React Material UI createStyles?

How do I change :hover status on an element when a different element is hovered over?

how do I setText value when I on click button?

How do I change a button's style property on hover?

Reactjs : How I can change button style when clicked

How do I use Selenium and Python to click on a button when the text changes to something different?

How to apply the style of the rest of the items when I click item in REACT?

How do I make a button act like a submit button for a form when it's in a different component in react?

I want to change the style in a item with a button click in the item, but when i click the button only the first item in the forech changes

How do I change the theme when I press the button? (React-native)

How do I change the style of an "<input>" element dynamically using Javascript?