Javascript DOM HTML Form name Property get

Introduction

Return the name of a form:

var x = document.getElementById("myForm").name;

Click button to return the value of the name attribute in the form element.

View in separate window

<!DOCTYPE html>
<html>
<body>

<form id="myForm" name="form1" action="/action_page.php">
  First name: <input type="text" name="fname" value="CSS"><br>
  Last name: <input type="text" name="lname" value="HTML"><br>
  <input type="submit" value="Submit">
</form>/*from   w  w w.ja v a  2  s .  com*/
<button onclick="myFunction()">Test</button>

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

<script>
function myFunction() {
  var x = document.getElementById("myForm").name;
  document.getElementById("demo").innerHTML = "The name of the form is: " + x;
}
</script>

</body>
</html>

The name property sets or gets the value of the name attribute in a form.

The name attribute specifies the name of a form.

Property Values

Value Description
name The name of the form

The name property returns a String representing the name of the form.




PreviousNext

Related