gibyalex
gibyalex

Reputation: 669

Javascript- How to disable a html button using javascript

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;

Upvotes: 1

Views: 694

Answers (4)

aarzoo
aarzoo

Reputation: 19

<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>

Upvotes: 1

John
John

Reputation: 107

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;
}

Upvotes: 0

Rajesh
Rajesh

Reputation: 24915

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>

Upvotes: 5

Apolo
Apolo

Reputation: 4050

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 !"/>

Upvotes: 2

Related Questions