467,219 Members | 1,125 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,219 developers. It's quick & easy.

How to disable and enable submit button in javascript

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
  • viewed: 4053
Share:
5 Replies
Expert 1GB
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
Thank you you are a star and you will go far.
Jan 20 '18 #3
Thank you great solution.
Feb 1 '18 #4
64KB
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>
Mar 5 '20 #5
gits
Expert Mod 4TB
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
Mar 5 '20 #6

Post your reply

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

Similar topics

5 posts views Thread by Jimbo | last post: by
4 posts views Thread by Dmitry Korolyov [MVP] | last post: by
3 posts views Thread by Jeff | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.