Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

148
Vistas
Cómo cambiar elementos HTML y redirigir a otra página

He estado aprendiendo desarrollo web front-end y pensé en desafiarme a mí mismo con un ejercicio, he creado un formulario HTML simple

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <form id="form"> <label for="name">Name:</label><br> <input type="text" id="name"><br> <label for="pass">Password:</label><br> <input type="password" id="pass"><br><br> <input type="submit" value="Submit"> </form> <script src="example.js"></script> </body> </html>

y para JavaScript, esto es lo que estoy usando

 const form = document.getElementById('form') const formName = document.getElementById('name') const formPass = document.getElementById('pass') const yourName = "John" const yourPass = "Doe" form.addEventListener('submit', function(e){ e.preventDefault() if (formName.value == yourName && formPass.value == yourPass) { document.querySelector('loginName').innerHTML = yourName; window.location.href = 'login.html'; } })

Lo que quiero aquí es que después de presionar enviar, la página web debería redirigir al .html de inicio de sesión que solo consiste en

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <h1 id="loginName"></h1> </body> </html>

Después de redirigir a login.html , también quiero que agregue la variable yourName de JavaScript a la etiqueta h1 , pero esto claramente no funciona.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

agregue <script src="example.js"></script> a su archivo login.html y cambie example.js a

 const formName = document.getElementById('name') const formPass = document.getElementById('pass') const yourName = "John" const yourPass = "Doe" if(document.URL.indexOf('example.html') != -1){ form.addEventListener('submit', function(e){ e.preventDefault() if (formName.value == yourName && formPass.value == yourPass) { window.location.href = 'login.html'; } }) } if(document.URL.indexOf('login.html') != -1){ document.getElementById('loginName').innerHTML = yourName; }
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda