Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Dec 2002
    Location
    Central New York
    Posts
    87
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need Help Getting Form's Input Value

    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?

    Thanks.

    Code:
      <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;
        }
      //-->
      </script>
    
        <form action="post.php" method="post" name="post_form">
          <div id="formHeader">Post New Thread</div>
          <fieldset id="name">
            <label>Name:</label>
            <input type="text" name="name" />
          </fieldset>
          <fieldset id="mail">
            <label>E-Mail:</label>
            <input type="text" name="mail" />
          </fieldset>
          <fieldset id="subj">
            <label>Subject:</label>
            <input type="text" name="subj" />
          </fieldset>
          <fieldset id="mesg">
            <label>Message:</label>
            <textarea name="body" id="body" cols="34" rows="5"></textarea>
          </fieldset>
          <fieldset id="btns">
          <script type="text/javascript">
          <!--
            document.write("<input type='button' class='button' value='Preview' " +
                           "onclick='formPreview();' />");
          //-->
          </script>
          <input type="submit" class="button" value="Post" />
          </fieldset>
        </form>
        <div id="preview"> </div>
    Timothy Boronczyk

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    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
    I am the luckiest man in the world


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •