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: https://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(()=>{
 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.
<h4>Example automatically Disable and Enable button</h4>
<p>Click on each button and wait 2 seconds.</p>

<button id='btn1'>Click</button> - <button id='btn2'>Click 2</button>
<script>
// function to disable and enable buttons, receives an array with button IDs
// from https://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(()=>{
 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>

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which attribute indicates that an input field must be filled out before submitting?
required="required" autofocus="autofocus" placeholder="text"
<input type="text" name="name1" required="required" />
Which CSS method increases or decreases the size of the HTML element (including its content)?
translate() scale() skew()
#some_id:hover {
  transform: scale(2, 1.5);
  -ms-transform: scale(2, 1.5);    /* IE 9 */
  -moz-transform: scale(2, 1.5);   /* Firefox */
}
Click on the Date object method that returns the day of the month.
setDate() getMonth() getDate()
var rightnow = new Date();
var day = rightnow.getDate();
alert(day);
Which function is used to upload a file on server?
is_file() move_uploaded_file() fopen()
if(move_uploaded_file($_FILES["field_name"]["tmp_name"], "dir/file_name")) {
  echo "The file succesfully uploaded";
}
Disable button and Enable it after specified time

Last accessed pages

  1. Node.js Move and Copy file (5418)
  2. Wake Up! (3403)
  3. Ajax-PHP Chat Script (37775)
  4. PHP-MySQL Scripts (3924)
  5. Create simple Website with PHP (20532)

Popular pages this month

  1. Insert, Select and Update NULL value in MySQL (415)
  2. Read Excel file data in PHP - PhpExcelReader (331)
  3. PhpSpreadsheet - Read, Write Excel and LibreOffice Calc files (257)
  4. PHP getElementById and getElementsByTagName (208)
  5. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (199)