The idea is that, on a page that contains a form, it redirects from the current page to the parent page when the form is filled. The problem is that it stays on the same page. The javaScript code is as follows:
function crearSesion(){
var user=document.getElementById("usuario").value;
var pass=document.getElementById("clave").value;
if(user!==""&& pass!==""){
if(user=='[email protected]' && pass=='javascript'){
sessionStorage.setItem("user2",pass);
alert("ingresado correctamente");
document.location.href="index.html";
}else{
alert("no estas registrado");
}
}
}
The function call from HTML5 code is as follows:
<input onclick="crearSesion();"type="submit" value="iniciar sesion" id="enviar">
Very grateful in advance.
The button does a
submit
and I think you don't want it to. For this reason, you must change thetype
and pass it tobutton
It is best to separate the JavaScript code from the HTML, that is, not to call functions within the HTML code itself.
As an example, starting from the following form in HTML.
The form could be perfectly validated in the following way, so you would separate what is the HTML code from the JavaScript code.