Javascript Form How to - Get text input field default value, value, name, type








Question

We would like to know how to get text input field default value, value, name, type.

Answer


<html>
<head>
    <script language="JavaScript">
    function openWin(){<!-- www .j ava2  s. co m-->
      var myInstance = document.myForm.myText;
      console.log("The defaultValue is: " + myInstance.defaultValue);
      console.log("The name is: " + myInstance.name + "<br>");
      console.log("The type is: " + myInstance.type + "<br>");
      console.log("The value is: " + myInstance.value + "<br>");
      console.log(myInstance.form.myButton.value);
    }
    </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: