View Full Version : Need Help Getting Form's Input Value

12-24-2003, 07:06 AM
The relavant portions of my code follow this post.

I'm able to obtain the value from the body textarea because it's labeled with an ID and I used getElementById(). But I also need to access other input values.. they don't use ID attributes, but do use name.

I've been trying but to no avail to get the values of the other inputs.

From what I've researched, document.post_form.mail.value should work, but doesn't. I've even tried document.post_form.mail.mail.value, thinking I need to traverse through the fieldset' ID. but still no luck. What am I doing wrong?


<script type="text/javascript">
function stripHTML(text) {
if (text) {
text = text.replace(/</g, "&lt;");
text = text.replace(/>/g, "&gt;");
return text;
else return null;

function formPreview()
text = document.getElementById('body').value;
text = stripHTML(text);
if (text) {
document.getElementById('preview').innerHTML = text.replace(/\r/g, "<br />;");
document.getElementById('preview').style.display = "block";
else document.getElementById('preview').style.display = "none";

return true;

<form action="post.php" method="post" name="post_form">
<div id="formHeader">Post New Thread</div>
<fieldset id="name">
<input type="text" name="name" />
<fieldset id="mail">
<input type="text" name="mail" />
<fieldset id="subj">
<input type="text" name="subj" />
<fieldset id="mesg">
<textarea name="body" id="body" cols="34" rows="5"></textarea>
<fieldset id="btns">
<script type="text/javascript">
document.write("<input type='button' class='button' value='Preview' " +
"onclick='formPreview();' />");
<input type="submit" class="button" value="Post" />
<div id="preview"> </div>

12-24-2003, 09:32 AM
the browser (at least IE does) sees the fieldset tags also as form elements. try alerting document.forms["post_form"].elements["mail"].length, in my IE, it returns 2, so the fieldset is found by it's id attribute, the input element is found by the name attribute and IE thinks there are 2 mail-inputs. Therefor it cannot get the value of this element.
try renaming the fieldset-id's (or the input-names) then:
document.forms["formname"].elements["elementname"].value should do the trick