4

Having issues with disabled={true} HTML button. App is in JavaScript + React. I set button initial value disabled={true} .For example purposes I will call it id="button1". Once I click another button let's say id=button2, I re-enable my button1 with code document.getElementById('button1').disabled=false;. Button re-enables but once clicked it has no functionality, it looks like it is still disabled. What am I doing wrong ?

OK here is my simplified code:

 function App() {

  const approve = () => {
    console.log('test');
    
  };

  const filterWeek2 = () => {
    document.getElementById('approve').removeAttribute("disabled");
  };


  return (
    <div className="App">
          <nav>
            <ul className="nav-area">
              <li><button id="Week2" onClick={filterWeek2}>WEEK 2</button></li>
              <li><button id="approve" onClick={approve} disabled="disabled">APPROVE</button></li> 
            </ul>            
          </nav>    
        </div>
  );
}

export default withAuthenticator(App);
1

1 Answer 1

2

The disabled is a boolean attribute, Its presence on an HTML elements disables the element. If it is present on the element, it will disable the HTML element, doesn't matter what value it has.

You have to remove the attribute using removeAttribute to make it editable

const disabled = document.querySelector(".disabled");
const normal = document.querySelector(".normal");

normal.addEventListener("click", () => {
  if (disabled.hasAttribute("disabled")) disabled.removeAttribute("disabled");
  else disabled.setAttribute("disabled", true);
})
<button disabled class="disabled">button</button>
<button class="normal">toggle</button>

Since you are using React, then you can toggle disabled using state. DEMO

disabled={active}

If active is true then the HTML element is disable else enabled.

Sign up to request clarification or add additional context in comments.

4 Comments

still does not work.
add your code in the question.
Added the code snippet
@Min Updated answer with sandbox link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.