.innerHTML in Javascript

I'm trying to check the login page with a div to display errors, I use innerHTML to change the empty text in the div with the error, the problem is that the msg error is written and disappears in just a second

here is the code

<html> <head> <title> Sign up for a membership @ Ay 7aga.com </title> <script type="text/javascript"> function check(){ var x = ""; if(document.getElementById('user').value =="") x+="Enter user name -"; if(document.getElementById('password').value =="") x+="Enter password"; document.getElementById('error1').innerHTML = x; } </script> </head> <body> <div name="error" id="error" style="width:1200px;background-color:red"> <p name="error1" id="error1"> </div> <center> <br/> <form name="register" id="register" > <table> <tr> <td> UserName </td> <td> <input type="text" id="user" /> </td> </tr> <tr> <td> Password </td> <td> <input type="password" name="password" id="password"/></td> </tr> </table> <button onclick="check()"> Submit </button> </form> <a href="Home page.html"> Go to Home page </a> </center> </body> </html> 
+4
source share
2 answers

When using the <button> it is important to use the "type" attribute: in most browsers (correctly), the "send" type is used by default, which makes it work as an element of the form presentation. Instead, you can set the type to "button", which causes the button to have its own behavior other than triggering event handlers. In this way:

In this way:

 <button onclick="check()" type=button>Submit</button> 

will hold the button from implicit form submission.

+2
source

You are approaching him wrong. It is likely that your request is sent after pressing the login button, so the text "disappears" after a couple of seconds.

Put your server login checks and create HTML content for this particular scenario instead of using JavaScript.

By the way: what <button> are you using?

+2
source

All Articles