Javascript Reference - HTML DOM Form name Property








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

The name attribute sets the name of a form and we can use the name value to reference a form in Javascript.

Browser Support

name Yes Yes Yes Yes Yes

Syntax

Return the name property.

var v = formObject.name

Set the name property.

formObject.name=name




Property Values

Value Description
name The name of the form

Return Value

A String, representing the name of the form.

Example

The following code shows how to get the name of a form.


<!DOCTYPE html>
<html>
<body>
<!--  w w  w  . jav a2  s .  c o m-->
<form id="myForm" name="form1" action="url">
  First name: <input type="text" name="fname" value="abc"><br>
  <input type="submit" value="Submit">
</form>
<button onclick="myFunction()">test</button>
<p id="demo"></p>

<script>
function myFunction() {
    var x = document.getElementById("myForm").name;
    document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to change the name of a form.


<!DOCTYPE html>
<html>
<body>
<!--from   w ww.j  a v a  2s. c  o m-->
<form id="myForm" name="form1" action="url">
  First name: <input type="text" name="fname" value="abc"><br>
  <input type="submit" value="Submit">
</form>
<button onclick="myFunction()">test</button>
<script>
function myFunction() {
    document.getElementById("myForm").name = "newName";
}
</script>
</body>
</html>

The code above is rendered as follows: