How to disable and enable submit button in javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dennyh
    New Member
    • Jan 2018
    • 2

    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.
  • Luuk
    Recognized Expert Top Contributor
    • Mar 2012
    • 1043

    #2
    I've changed your request from minutes to seconds, but you can change that yourself with the following code

    Code:
    <!DOCTYPE html>
    <html>
    <body onload="setInterval(myFunction,100)">
    
    <p>Click the button to display the minutes of the time right now.</p>
    
    <button id="mybutton" onclick="myFunction()">Try it</button>
    
    <p id="demo"></p>
    
    <script>
    function myFunction() {
        var d = new Date();
        var nh = d.getHours();
        var nm = d.getMinutes();
        var ns = d.getSeconds();
        document.getElementById("demo").innerHTML = (nh*100+nm)*100+ns;
        var t = ns;
        document.getElementById("mybutton").innerHTML = t % 10;
        if ((t % 10) >=8 && (t % 10) <10) 
            document.getElementById("mybutton").disabled = true
        else 
            document.getElementById("mybutton").disabled = false
    }
    </script>
    
    </body>
    </html>

    Comment

    • Dennyh
      New Member
      • Jan 2018
      • 2

      #3
      Thank you you are a star and you will go far.

      Comment

      • RockybBalboa
        New Member
        • Feb 2018
        • 5

        #4
        Thank you great solution.

        Comment

        • Sherin
          New Member
          • Jan 2020
          • 77

          #5
          The Code
          Code:
          <html>
          <head>
              <title>Enable/Disable Submit Button based on Multiple Textbox values</title>
          </head>
          <body>    
              <p>Enter some values in the text to Enable the Submit button!</p>
          
              <p>Name: <input type="text" id="name" placeholder="Enter your name" 
                  onchange="manage(this)" /></p>
              Designation: <input type="text" id="desig" placeholder="Enter designation" 
                  onchange="manage(this)" />
          
              <input type="submit" id="submit" disabled />
          </body>
          
          <script>
              function manage(txt) {
                  var bt = document.getElementById('submit');
                  var ele = document.getElementsByTagName('input'); 
          
                  // LOOP THROUGH EACH ELEMENT.
                  for (i = 0; i < ele.length; i++) {
          
                      // CHECK THE ELEMENT TYPE.
                      if (ele[i].type == 'text' && ele[i].value == '') {
                          bt.disabled = true;    // Disable the button.
                          return false;
                      }
                      else {
                          bt.disabled = false;   // Enable the button.
                      }
                  }
              }    
          </script>
          </html>

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5388

            #6
            The Code

            Code:
            <html>
            <head>
                <title>Enable/Disable Submit Button based on Multiple Textbox values</title>
            </head>
            <body>    
                <p>Enter some values in the text to Enable the Submit button!</p>
             
                <p>Name: <input type="text" id="name" placeholder="Enter your name" 
                    onchange="manage(this)" /></p>
                Designation: <input type="text" id="desig" placeholder="Enter designation" 
                    onchange="manage(this)" />
             
                <input type="submit" id="submit" disabled />
            </body>
             
            <script>
                function manage(txt) {
                    var bt = document.getElementById('submit');
                    var ele = document.getElementsByTagName('input'); 
             
                    // LOOP THROUGH EACH ELEMENT.
                    for (i = 0; i < ele.length; i++) {
             
                        // CHECK THE ELEMENT TYPE.
                        if (ele[i].type == 'text' && ele[i].value == '') {
                            bt.disabled = true;    // Disable the button.
                            return false;
                        }
                        else {
                            bt.disabled = false;   // Enable the button.
                        }
                    }
                }    
            </script>
            </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
            Last edited by gits; Mar 6 '20, 11:03 AM. Reason: make 'The Code' persistent for those that might be interested in the mentioned flaws

            Comment

            Working...