Cuando uso el elemento de formulario, aparece un error que dice que no puede establecer las propiedades en undefined .
html:
<form name="regForm"> <table> <tr> <!-- First Name --> <td> <input id="firstName" type="text" placeholder="First Name"> </td> </tr> <tr> <!-- Error Box --> <td> <p id="returnOutput"></p> </td> </tr> <tr> <!-- Submit button --> <td> <button type="button" onclick="validateForm()">Sign Up</button> </td> </tr> </table> </form>JavaScript:
function validateForm() { var firstName = regForm.firstName.value; regForm.returnOutput.value = firstName; } Esto es para una evaluación, por lo que debe hacerse de esta manera; de lo contrario, estaría usando document.getElementById().value
¡Bienvenido a StackOverflow!
regForm no está definido; necesitas definirlo primero.
Solo un consejo: use const para valores que no cambiarán, ¡es una buena práctica!
function validateForm() { // SELECT regForm by it's name. It doesn't have an ID, so we have to select it by name. However, if it did have an ID, we can use it. 0 is just to select the first element it finds since getElementsByName returns a node list const regForm = document.getElementsByName("regForm")[0] // Declare firstName as the firstname.value const firstName = regForm.firstName.value; // Finally, append it to body :) document.getElementById("returnOutput").innerHTML = firstName; }Espero que esto haya ayudado :)