Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

HTML

[SOLVED]Creating a submit button that outputs the input that has been written in by the user onto the same page.

Hello, this is my code. The objective is to output what the user has written in underneath my form but I am having a few issues where only the first and last name are being outputted.

<form method="post" id="name-form">
First Name: <input type="text" name="first_name"/> <br/><br/> Last Name: <input type="text" name="last_name"/> <br/><br/> Email: <input type="text" name="email"/> <br/><br/> Comment: <textarea name="comment" rows="5" cols="40"></textarea> <br/><br/> Gender: <input type="radio" name="gender" value="female"/>Female <input type="radio" name="gender" value="male"/>Male <br/><br/> <input name="Submit1" type="submit" value="submit" /> </form>

<tr> <td style="width:50px" >Welcome:</td> <td > <label id="welcome" /> </td> <td> email: address</td> <td > <label id="email" /> </td> <td> comment:</td> <td > <label id="comment" /> </td> <td> gender:</td> <td > <label id="gender" /> </td> </tr>

<script> var form = document.getElementById('name-form'); form.onsubmit = function(event) { event.preventDefault(); var welcome = document.getElementById('welcome'); var email = document.getElementById('email1'); var comment = document.getElementById('comment1'); var gender = document.getElementById('gender');

welcome.innerHTML =  form.first_name.value + " " + form.la;st_name.value;
email.innerHTML = document.getElementsByName('email').value 
var text =  document.getElementsByName('comment').value;
comment.innerHTML= text; 
gender.innerHTML =  form.gender.value; 

form.reset();

2 Answers

Thanks for all your help! I managed to fix it. Here is my code!

    </table>

<form method="post" id="name-form">
Full Name: <input type="text" name="full_name"/>

<br/><br/> Email:<input type="text" name="email"/>

<br/><br/>

        Comment: <textarea name="comment" rows="4" cols="40"></textarea> 

<br/><br/>

Gender: <input type="radio" name="gender" value="female"/>Female <input type="radio" name="gender" value="male"/>Male

<br/><br/>

        <input name="Submit1" type="submit" value="submit" /> 

</form>

<div id="results"> </div> <div> <label id="email"></label></div> <div><label id="comment"></label></div> <script> var form = document.getElementById('name-form'); form.onsubmit = function(event) { event.preventDefault(); var results = document.getElementById('results');

    results.innerHTML = "Your gender is: " + form.gender.value + "; "; 
    results.innerHTML += " Your full Name is " + form.full_name.value + "; your email is ";
    document.getElementById('email').innerHTML = form.email.value + " ";
    document.getElementById('comment').innerHTML = form.comment.value + " ";
    form.reset();

}

</script>

jeffrey bachynski
jeffrey bachynski
5,179 Points

Glad to see that you got it working :)

jeffrey bachynski
jeffrey bachynski
5,179 Points

There are a few things going on, to start with,

  • Some of your getElementById calls don't match the ids in the the Html.

    <label id="email" />  does not match   var email = document.getElementById('email1');  
    <label id="comment" /> does not match   var comment = document.getElementById('comment1');
    

    They both return null.

  • Also, your output table is maybe not displaying properly because the tr and td tags are not surrounded by a table tag

eg

<table>
  <tr>
    <td>Cell A</td>
    <td>Cell B</td>
  </tr>
</table>

https://www.w3schools.com/tags/tag_td.asp for more information.

  • One more thing, the getElementsByName method returns a collection as a NodeList object and it can be accessed by an index starting with 0.

For example you could use

            email.innerHTML = document.getElementsByName('email')[0].value 

to access the value in <input type="text" name="email"/> input field. For more info on getElementsByName https://www.w3schools.com/jsref/met_doc_getelementsbyname.asp. If working with NodeList objects and indexes seem confusing, you might want to give the element an id, and then you use that instead.

That should get you started. If you get stuck on other elements, W3 schools is a good place to get example code and try it out. If you make those changes, it should work. I got your code working, so if can be done. If you still get stuck on something further, don't be shy. Good Luck and happy coding.

Thank you for all your help. I changed my code round and everything works except from my comment section because it's a textarea it isn't displaying.

<form method="post" id="name-form">
Full Name: <input type="text" name="full_name"/>

<br/><br/> Email:<input type="text" name="email"/>

<br/><br/>

        Comment: <textarea name="comment" rows="4" cols="40"></textarea> 

<br/><br/>

Gender: <input type="radio" name="gender" value="female"/>Female <input type="radio" name="gender" value="male"/>Male

<br/><br/>

        <input name="Submit1" type="submit" value="submit" /> 

</form>

<div id="results"> </div> <div> <label id="email"></label></div>

<script> var form = document.getElementById('name-form'); form.onsubmit = function(event) { event.preventDefault(); var results = document.getElementById('results');

    results.innerHTML = "Your gender is: " + form.gender.value + "; "; 
    results.innerHTML += " Your full Name is " + form.full_name.value;
    document.getElementById('email').innerHTML = form.email.value + " Is your email" /*document.getElementsByName('email').value; */
    document.getElementById('comment').innerHTML = form.comment.value; 
    var x = document.getElementsByName("comment").value;
    document.getElementById("comment").innerHTML = x;
    form.reset();

}