By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,548 Members | 1,183 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,548 IT Pros & Developers. It's quick & easy.

How to disable and enable submit button in javascript

P: 2
Hello,
How do I disable a submit button for 2 minutes and then enable it from 10 minutes past from the hour i.e 12:08 to 12:10 disable button and then enable from 12:10 to 12:18 disable from 12:18 to 12:20 and so on around the clock. Can this be done in JavaScript. Thanks for your help.
Jan 19 '18 #1
Share this Question
Share on Google+
5 Replies


Expert 100+
P: 1,039
I've changed your request from minutes to seconds, but you can change that yourself with the following code

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html>
  2. <html>
  3. <body onload="setInterval(myFunction,100)">
  4.  
  5. <p>Click the button to display the minutes of the time right now.</p>
  6.  
  7. <button id="mybutton" onclick="myFunction()">Try it</button>
  8.  
  9. <p id="demo"></p>
  10.  
  11. <script>
  12. function myFunction() {
  13.     var d = new Date();
  14.     var nh = d.getHours();
  15.     var nm = d.getMinutes();
  16.     var ns = d.getSeconds();
  17.     document.getElementById("demo").innerHTML = (nh*100+nm)*100+ns;
  18.     var t = ns;
  19.     document.getElementById("mybutton").innerHTML = t % 10;
  20.     if ((t % 10) >=8 && (t % 10) <10) 
  21.         document.getElementById("mybutton").disabled = true
  22.     else 
  23.         document.getElementById("mybutton").disabled = false
  24. }
  25. </script>
  26.  
  27. </body>
  28. </html>
  29.  
Jan 20 '18 #2

P: 2
Thank you you are a star and you will go far.
Jan 20 '18 #3

P: 5
Thank you great solution.
Feb 1 '18 #4

P: 6
The Code
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3.     <title>Enable/Disable Submit Button based on Multiple Textbox values</title>
  4. </head>
  5. <body>    
  6.     <p>Enter some values in the text to Enable the Submit button!</p>
  7.  
  8.     <p>Name: <input type="text" id="name" placeholder="Enter your name" 
  9.         onchange="manage(this)" /></p>
  10.     Designation: <input type="text" id="desig" placeholder="Enter designation" 
  11.         onchange="manage(this)" />
  12.  
  13.     <input type="submit" id="submit" disabled />
  14. </body>
  15.  
  16. <script>
  17.     function manage(txt) {
  18.         var bt = document.getElementById('submit');
  19.         var ele = document.getElementsByTagName('input'); 
  20.  
  21.         // LOOP THROUGH EACH ELEMENT.
  22.         for (i = 0; i < ele.length; i++) {
  23.  
  24.             // CHECK THE ELEMENT TYPE.
  25.             if (ele[i].type == 'text' && ele[i].value == '') {
  26.                 bt.disabled = true;    // Disable the button.
  27.                 return false;
  28.             }
  29.             else {
  30.                 bt.disabled = false;   // Enable the button.
  31.             }
  32.         }
  33.     }    
  34. </script>
  35. </html>
3 Weeks Ago #5

gits
Expert Mod 5K+
P: 5,387
The Code

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3.     <title>Enable/Disable Submit Button based on Multiple Textbox values</title>
  4. </head>
  5. <body>    
  6.     <p>Enter some values in the text to Enable the Submit button!</p>
  7.  
  8.     <p>Name: <input type="text" id="name" placeholder="Enter your name" 
  9.         onchange="manage(this)" /></p>
  10.     Designation: <input type="text" id="desig" placeholder="Enter designation" 
  11.         onchange="manage(this)" />
  12.  
  13.     <input type="submit" id="submit" disabled />
  14. </body>
  15.  
  16. <script>
  17.     function manage(txt) {
  18.         var bt = document.getElementById('submit');
  19.         var ele = document.getElementsByTagName('input'); 
  20.  
  21.         // LOOP THROUGH EACH ELEMENT.
  22.         for (i = 0; i < ele.length; i++) {
  23.  
  24.             // CHECK THE ELEMENT TYPE.
  25.             if (ele[i].type == 'text' && ele[i].value == '') {
  26.                 bt.disabled = true;    // Disable the button.
  27.                 return false;
  28.             }
  29.             else {
  30.                 bt.disabled = false;   // Enable the button.
  31.             }
  32.         }
  33.     }    
  34. </script>
  35. </html>
ok - since 'the code' is constantly posted regardless of being not useful in this context (since its not related to the OP's requirements) lets use it as an example of how not to write Javascript code (even for the 'selfchosen' example the poster decided to throw out code for):

1. function has a meaningless name
2. function gets a parameter passed that is never used
3. the variable i is an implicit global
4. wrong inline comment
5. return value not used but return misused to break out of the loop
6. loop is unnecessarily evaluating length in each iteration
7. element lookup in loop happens twice instead of 1 time only per iteration
3 Weeks Ago #6

Post your reply

Sign in to post your reply or Sign up for a free account.