简体   繁体   中英

Reenable button onClick

I tried to disable my button with an onClick function using this:

document.getElementById("btn").onClick = null;

How can I reenable it back again? example:

if(some condition)
  document.getElementById("btn").onClick = null;
else
  //reenable it again

Try like this

disable

document.getElementById("btn").disabled=true;

enable

document.getElementById("btn").disabled=false;

Simple way is that save old onclick's method to value ( tempFn ). Then you can reenable again.

 var tempFn; document.getElementById("btn2").onclick = function () { if (!tempFn) {//some condition tempFn = document.getElementById("btn").onclick; document.getElementById("btn").onclick = null; } else { document.getElementById("btn").onclick = tempFn; tempFn = null; } } document.getElementById("btn").onclick = function () { alert("btn"); } 
 <button id="btn">btn</div> <button id="btn2">Disable/Enable</div> 

You can do it with jquery

    $(function(){
         if(smth){
          //Set button disabled
          $("#btn").attr("disabled", "disabled");
}else{
     $("#btn").removeAttr("disabled");  
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM