Online Shopping : Computers : Programming : Languages : JavaScript : Forms : Buttons

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

111 of 155 people (72%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

How can I disable a button after the first click?

Jun 21st, 2001 01:34
Martin Honnen,


The following code used the 
  disabled 
property supported in NN6 and IE4+ and uses some self created 
  clicked
property for other browsers:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>
button disabling
</title>
<script type="text/javascript">
function toggleButton (button) {
  if (typeof button.disabled != "undefined")
    button.disabled = !button.disabled;
  else if (button.clicked) {
    button.clicked = false;
    button.value = button.oldValue;
  }
  else {
    button.clicked = true;
    button.oldValue = button.value;
    button.value = 'DISABLED';
  }
}
</script>
</head>
<body>
<form name="formName">
  <input type="button" onclick="if (!this.clicked) {
                    alert('Kibology'); // example of click handler
                    toggleButton(this);
                  }"
         name="aButton"
         value="button"
   />
   <input type="button" value="toggle button state"
          onclick="toggleButton(this.form.aButton);"
   />
</form>
</body>
</html>