please check my JS form


Question is:

Create an HTML document that contains two forms. One form consists of a text
field, the other a text area. Name the forms and the input devices. Use Java-
Script to print out the names and values in the forms.

However my code gives undefined on the values - can someone please advise.


<head><title>Id and Name Attributes</title>



<form name="form1">

<input type="text" name="text1" value="value"/>


<form name="form2">

<input type="textarea" name="text2" value="value"/>


<script type="text/javascript">
document.writeln("The name of the first form is " +;
document.writeln("The name of the second form is " +;
document.writeln("The name of the first form is " + document.form1.value);
document.writeln("The name of the second form is " + document.form2.value);

Who is Participating?

Improve company productivity with a Business Account.Sign Up

Lalit ChandraConnect With a Mentor Commented: will get the control reference only,and you are trying to get concatenate that with the string.You should use VALUE to extract the value of the html text box control.
Also, document.form is somewhat deprecated.
 it's better to use document.form[0] instead.
Lalit ChandraCommented:
Try this

user_input = document.forms[0].text.value

where text is the name of the text field, textarea or hidden field. The value of this element gives the text
jecommeraAuthor Commented:

Thanks for this however I am trying to understand this behaviour.

i.e. why is it giving back undefined instead of the value?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.