Javascript Form How to - Get form in which the text box is contained








Question

We would like to know how to get form in which the text box is contained.

Answer


<!--from   www.ja  va2s .c om-->
<html>
<head>
    <script language="JavaScript">
    function openWin(){
      var formData = document.myForm.myText.form;
      console.log("The name of the form is: " + formData.name + "<br>");
      console.log(formData.myText.value + "<br>");
      console.log(formData.elements[1].name + "<br>");
    }
    </script>
</head>
<body>
    <form name="myForm">
      <input type=TEXT value="hello world" name="myText">
      <input type=BUTTON value="Click to Process" name="myButton"
             onClick="openWin()">
    </form>
</body>
</html>

The code above is rendered as follows: