JavaScript Auto submit form using timer auto submit form on page load,auto submit form javascript,

I wrote the following code. With this code pushing Submit button submits the form manually. I have also a timer which I want to auto submit the form after 10 seconds. But it does not work. It counts until 0 and then it does not do anything. Can you please tell me what I am missing or how to change my timer (if there, is the problem)? But I want the user to watch the timer as my example

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script type="text/javascript">
    function countDown(secs,elem)
    {
        var element = document.getElementById(elem);
        element.innerHTML = "<h2>You have <b>"+secs+"</b> seconds to answer the questions</h2>";
        if(secs < 1){
        clearTimeout(timer);
        document.getElementById('myquiz').submit();
        }
        secs--;
        var timer = setTimeout ('countDown('+secs+',"'+elem+'")',1500);
    }
</script> 
<div id="status"></div>
<script type="text/javascript">countDown(5,"status");</script>
 <title>Questionnaire</title>
<style type="text/css"> 
    span {color: #FF00CC}
</style>
</head>
<body>
<h1>Please complete the following Survey</h1>
<form name="quiz" id ="myquiz" method="post" action="includes/process.php">
First Name: <input type="text" name="firstname" id="fname"/>
<p></p>
Last Name: <input type="text" name="lastname" id="lname"/>
<p></p>
<input type="submit" name="submit" value="Go"></input>
<input type="reset" value="clear all"></input>
</form>
</body>
</html>
Answer:1

There are three errors producing this bug.

  • You have a typo in form attributes. there is a space after id. It should be id="myquiz".
  • Your form has a button named "submit", which is wrong. It overrides the function. Name it "submitbutton" or something other.
  • The "validate" method is not defined. It should return true.

By the way, the timeout has wrong time, it should be 1000.

Working example: plunk

Answer:2

You have to submit a form, not an element so try this:

document.forms["quiz"].submit();

OR if its the only form you can use

document.forms[0].submit();
Answer:3

This Code will work:

  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
            <script type="text/javascript">
            function countDown(secs, elem)
            {
                var element = document.getElementById(elem);
                element.innerHTML = "<h2>You have <b>"+secs+"</b> seconds to answer the questions</h2>";
                if(secs < 1) {
                    document.quiz.submit();
                }
                else
                {
                    secs--;
                    setTimeout('countDown('+secs+',"'+elem+'")',1500);
                }
            }

            function validate() {
                return true;
            }
            </script> 
            <div id="status"></div>
            <script type="text/javascript">countDown(5,"status");</script>
            <title>Questionnaire</title>
            <style type="text/css"> 
            span { 
                color: #FF00CC;
            }
            </style>
        </head>
        <body>
            <h1>Please complete the following Survey</h1>
            <form name="quiz" id="myquiz" onsubmit="return validate()" method="post" action="includes/process.php">
                First Name: <input type="text" name="firstname" id="fname"/>
                <p></p>
                Last Name: <input type="text" name="lastname" id="lname"/>
                <p></p>
                <input type="submit" name="submitbutton" value="Go"></input>
                <input type="reset" value="clear all"></input>
            </form>
        </body>
    </html>
Answer:4

Is there any way to map keys and values ​​in an array using Javascript? In my opinion, it is something similar to jQuery .map(), but instead of mapping only the value, also "maps" the keys. Suppose I ...

Is there any way to map keys and values ​​in an array using Javascript? In my opinion, it is something similar to jQuery .map(), but instead of mapping only the value, also "maps" the keys. Suppose I ...

  1. mapping key value
  2. elasticsearch mapping key value
  3. javascript mapping key value
  4. c# mapping key value
  5. hashmap mapping key value
  6. hash mapping key value
  7. key value mapping python
  8. key value mapping in java
  9. key value mapping in excel
  10. key value mapping apigee

What is o ? If I am not mistaken, prototype means that F is going to inherit from o , but that is all I can understand here. Can someone break this down for me: if (!Object.create) { Object....

What is o ? If I am not mistaken, prototype means that F is going to inherit from o , but that is all I can understand here. Can someone break this down for me: if (!Object.create) { Object....

I have a directive which contains an element inside it with width: 100%. However, the directive must know the clientWidth of that element -- that is, in pixels rather than percent. The element is ...

I have a directive which contains an element inside it with width: 100%. However, the directive must know the clientWidth of that element -- that is, in pixels rather than percent. The element is ...

  1. get element width javascript
  2. get element width
  3. get element width js
  4. get element width jquery
  5. get element width react
  6. get element width css
  7. get element width angular
  8. get element width and height javascript
  9. get element width without padding
  10. get element width with margin
  11. get element width angular 6
  12. get element width vue
  13. get element width react native
  14. get element width vanilla js
  15. get element width vuejs
  16. get element width and height
  17. get element width without padding javascript
  18. get element width html
  19. get element width javascript in pixels
  20. get element width typescript

I need to know if an element is styled with a :first-letter style, and it should be a general solution so I won't depend on class names or special style attributes. Is there any way? Example: <p ...

I need to know if an element is styled with a :first-letter style, and it should be a general solution so I won't depend on class names or special style attributes. Is there any way? Example: <p ...