Javascript Form How to - Access form which contains the TextArea from TextArea object








Question

We would like to know how to access form which contains the TextArea from TextArea object.

Answer


    <!--from  w w  w  . j  a  v a2s.  c om-->


<html>
<head>
    <script language="JavaScript">
    function openWin(){
      var formData = document.myForm.myTextArea.form;
      
      console.log("The name of the form is: " + formData.name + "<br>");
      console.log(formData.myTextArea.value + "<br>");
      console.log(formData.elements[1].name + "<br>");
    }
    </script>
    </head>
    <body>
    <form name="myForm">
       <textarea name="myTextArea" rows=6 cols=50>
       Here is some text in my text area.
       </textarea>
    <input type=BUTTON value="Click to Process" name="myButton" onClick="openWin()">
    </form>
</body>
</html>

The code above is rendered as follows: