1

How to disable a html button on JavaScript onclick event ? I am using a input type="button"

<input type="button" class="standardbutton" value="Password Reset" id="mybutton" onclick=" passwordReset()"/>

Since I am not using jquery or any framework for the UI, how to disable it?

I tried to set disabled = true, but it didn't work.

For example:

 document.getElementById("mybutton").disabled = true;
8
  • Add the relevant code to your question, so that we can see what is going on. Commented Jan 20, 2016 at 10:06
  • a jsfiddle would be nice as well Commented Jan 20, 2016 at 10:07
  • add relevant code to understand problem Commented Jan 20, 2016 at 10:11
  • juqery can help you to do it very quickly Commented Jan 20, 2016 at 10:11
  • 1
    @willie document.querySelector('#mybutton').disabled = true how is that any more slower or complicated than fetchnig jQuery just to use its attr()? Commented Jan 20, 2016 at 10:15

4 Answers 4

5

Works fine for me

function disableBtn1(){
  document.getElementById("btn1").disabled = true;
}
<input type="button" id="btn1" value="Btn 1"/>
<button id="btn2" onclick="disableBtn1();"> btn 2</button>

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

Comments

2

Set disabled=true should work, you may have an error somewhere else :

function toggle_disable() {
  var button = document.getElementById("test");

  if (button.disabled) {
    button.disabled = false;
  } else {
    button.disabled = true;
  }
}
<input type="button" id="test" value="I'm a button"/>

<input type="button" id="toggle" onclick="toggle_disable()" value="&lt;= Disable it !"/>

Comments

1
<button id="button">My Button</button>

<p>Click the button below to disable the button above.</p>

<button onclick="disable()">Click</button>

<script>
  function disable() {
    document.getElementById("button").disabled = true;
  }
</script>

Comments

0

Set disabled = true in javascript function

     <input type="button" class="standardbutton" value="Password Reset" id="mybutton" onclick=" passwordReset()"/>

In JS

function passwordReset(){
  document.getElementById("mybutton").disabled = true;
}

Comments

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.