Javascript Course

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 tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
Disable button and Enable it after specified time

Last accessed pages

  1. Mysql SELECT JOIN tables on two different Databases (4498)
  2. jQuery UI draggable - Drag elements (11448)
  3. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (142520)
  4. Using the Bone Tool (4253)
  5. Node.js Move and Copy Directory (20134)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (523)
  2. The Mastery of Love (65)
  3. CSS cursor property - Custom Cursors (62)
  4. Read Excel file data in PHP - PhpExcelReader (59)
  5. PHP-MySQL free course, online tutorials PHP MySQL code (44)