Form elements Collection namedItem(id) - Get the value of the element with name="fname" in a form: - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Form

Description

Form elements Collection namedItem(id) - Get the value of the element with name="fname" in a form:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<form id="myForm" action="/action_page.php">
  First name: <input type="text" name="fname" value="Donald"><br>
  Last name: <input type="text" name="lname" value="Duck"><br>
  <input type="submit" value="Submit">
</form>//from  w ww.  ja v a  2s .  com

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {
    var x = document.getElementById("myForm").elements.namedItem("fname").value;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

Related Tutorials