ngreenwood6 Posted October 2, 2008 Share Posted October 2, 2008 I am a noob to javascript and am in the process of learning. However, I have run into a problem. I have defined a function that gets the name from a form that is submitted. When the form is submitted I want it to print the results to the same page that the form is on underneath the form. I have the following code: <form name="form"> <input type="text" name="new_name" /> <input name="submit" value="submit" type="button" onclick="submit_form();" /> <br /> <br /> </form> <script type="text/javascript"> function submit_form() { //get the users name from the form var users_name = document.form.new_name.value; } document.write(users_name); </script> However it is not posting it to the page. If I put the document.write(users_name); inside the function it prints it to the page but it prints it to a whole new page instead of the one with the form on it. Again I am a noob so please take it easy. Thanks in advance for the help Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted October 2, 2008 Share Posted October 2, 2008 Put it in the form but think about it a bit more. Probably the best way to do this is using the <span> tag. Then you can set the inner html. Like so: <script language="javascript"> function submit_form(){ var name = document.getElementById("name").value; //Now set the values document.getElementById("writing").innerHTML = name; } </script> <input type="text" name="name" id="name" /><br /> <input type="submit" name="submit" id="submit" value="Submit Form" onclick="javascript: submit_form()" /><br /><br /> <span id="writing"></span> Start using things like getElementById and get in the habit of assigning fields with unique id's. Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted October 2, 2008 Author Share Posted October 2, 2008 Wow thanks for the help. The getelementbyid seems alot more useful, easier to use, and understand. again thanks for the help Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted October 2, 2008 Share Posted October 2, 2008 Remember that it is case sensitive. It bust be written as getElementById() Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted October 2, 2008 Author Share Posted October 2, 2008 yeah i have that just didnt feel like doing the caps in the post. Quote Link to comment Share on other sites More sharing options...
Flames Posted October 3, 2008 Share Posted October 3, 2008 I know its solved but don't you think it would be better if you didnt name a topic HELP!!but rather what you need help with? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.