The function presented in this page can be used to disable and automatically enable buttons in web page, with JavaScript. After the user clicks on the button, that button is disabled, and its text is changed, then, after 2 seconds, the button is automatically enabled.
- This function receives an Array with the ID of the buttons to which you want to add this effect.

Code of the function:
// function to disable and enable buttons, receives an array with button IDs
// from http://coursesweb.net/javascript/
function disableEnableBtn(ids) {
  // traverses the array with IDs
  var nrids = ids.length;
  for(var i=0; i<nrids; i++) {
    // registers onclick event to each button
    if(document.getElementById(ids[i])) {
      document.getElementById(ids[i]).onclick = function() {
        this.setAttribute('disabled', 'disabled');     // disables the button by adding the 'disabled' attribute
        this.innerHTML = 'Disabled';        // changes the button text
        var idbtn = this.id;       // stores the button ID

        // calls a function after 2 sec. (2000 milliseconds)
        setTimeout( function() {
          document.getElementById(idbtn).removeAttribute('disabled');         // removes the "disabled" attribute
          document.getElementById(idbtn).innerHTML = 'Click';        // changes tne button text
        }, 2000 );
      }
    }
  }
}
- Set an array with IDs of the buttons (one or more IDs), then call the disableEnableBtn() function, passing the array as argument. To change the time after which the button is enabled, modify the value of 2000 added in setTimeout(); see also the comments in code.

- Example with two buttons.
<button id="btn1">Click</button> - <button id="btn2">Click 2</button>
<script type="text/javascript">
// function to disable and enable buttons, receives an array with button IDs
// from http://coursesweb.net/javascript/
function disableEnableBtn(ids) {
  // traverses the array with IDs
  var nrids = ids.length;
  for(var i=0; i<nrids; i++) {
    // registers onclick event to each button
    if(document.getElementById(ids[i])) {
      document.getElementById(ids[i]).onclick = function() {
        this.setAttribute('disabled', 'disabled');     // disables the button by adding the 'disabled' attribute
        this.innerHTML = 'Disabled';        // changes the button text
        var idbtn = this.id;       // stores the button ID

        // calls a function after 2 sec. (2000 milliseconds)
        setTimeout( function() {
          document.getElementById(idbtn).removeAttribute('disabled');         // removes the "disabled" attribute
          document.getElementById(idbtn).innerHTML = 'Click';        // changes tne button text
        }, 2000 );
      }
    }
  }
}

// array with IDs of buttons
var btnid = ['btn1', 'btn2'];

disableEnableBtn(btnid);      // calls the function
</script>
Demo, click on the button and wait 2 seconds.
-

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag adds a new line into a paragraph?
<b> <br> <p>
First line ...<br>
Other line...
Which CSS property can be used to add space between letters?
text-size word-spacing letter-spacing
#id {
  letter-spacing: 2px;
}
What JavaScript function can be used to get access to HTML element with a specified ID?
getElementById() getElementsByTagName() createElement()
var elm = document.getElementById("theID");
var content = elm.innerHTML;
alert(content);
Click on the "echo" correct instruction.
echo "CoursesWeb.net" echo "CoursesWeb.net"; echo ""CoursesWeb.net";
echo "Address URL: http://CoursesWeb.net";
Disable button and Enable it after specified time

Last accessed pages

  1. Select in MySQL, Output results in HTML Table (1419)
  2. Golf Putt It In (739)
  3. Disable button and Enable it after specified time (5129)
  4. Flash Games - Free online Games (22975)
  5. Move image from an element /tag to another (1637)

Popular pages this month

  1. Qwop (3219)
  2. Get Attribute (ID, Class, Name, Title, Src) with jQuery (2547)
  3. Get the value of the selected /checked checkboxes in a form (1608)
  4. Insert, Select and Update NULL value in MySQL (1423)
  5. Add, Change, and Remove Attributes with jQuery (1301)