- 1
Beginner problem with javascript code not working.
Hi, thanks for passing by :) i have the following code and i can't find the reason why it isn't working. I'll paste it. Basically i want the user to enter two lines of text, any text and have it pop up in an alert message when the submit button is clicked. Any help or questions are welcomed. <form onsubmit = "return cargar() method= post> Nombre o E-mail <input type="text" name= "nom" id="nom"> <br> Comentarios <input type="text" name="com" id="com"> <br> <input type="submit" value="Submit" /> </form> <script type= "text/javascript"> function cargar(){ var nom= document.getElementById(nom); var com= document.getElementById(com); alert (nom_value + " "+ com_value); } </script>
4 ответов
+ 5
Maybe try to use an <input type="button"> and its 'onclick' event attribute, rather than the 'onsubmit' event attribute of <form> element: 'submit' button have special behaviour that could make load another document ;)
Except from that, there's no reason that your corrected code will not work (even elsewhere appart code playground ^^)...
+ 1
On this line:
<form onsubmit = "return cargar() method= post>
You're missing a closing quotation " for the onsubmit=
Also, for how you're using it here return isn't needed, but should work either way.
<form onsubmit = "cargar()" method= "post">
For these 2 lines:
var nom= document.getElementById(nom);
var com= document.getElementById(com);
You first need to put nom and com (the id's) in quotes as well
You should then change their variable names (or not and change them in the alert call) so that they match what you're using in the alert function. You can assign the value directly by appending .value to each assignment
var nom_value = document.getElementById("nom").value;
var com_value = document.getElementById("com").value;
<form onsubmit ="cargar()" method="post">
Nombre o E-mail <input type="text" name="nom" id="nom">
<br>
Comentarios <input type="text" name="com" id="com">
<br>
<input type="submit" value="Submit" />
</form>
<script type= "text/javascript">
function cargar() {
var nom_value = document.getElementById("nom").value;
var com_value = document.getElementById("com").value;
alert (nom_value + " "+ com_value);
}
</script>
+ 1
Thanks for replying! i've corrected the code but still doesn't work. When i click the submit button still nothing happens. Even if i don't enter any text in the input box. Here's the code corrected:
<form onsubmit ="cargar()" method= post>
Nombre o E-mail <input type="text" name= "nom" id="nom">
<br>
Comentarios <input type="text" name="com" id="com">
<br>
<input type="submit" value="Submit" />
</form>
<script type= "text/javascript">
function cargar(){
var nom_value= document.getElementById("nom").value;
var com_value= document.getElementById("com").value;
alert (nom_value + " "+ com_value);
0
I copied the code from your last post and just added the closing curly brace for the cargar function and the closing script tag and added it between the body tags and it worked fine for me. At least in the code playground.