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.
name |
Yes | Yes | Yes | Yes | Yes |
Return the name property.
var v = formObject.name
Set the name property.
formObject.name=name
Value | Description |
---|---|
name | The name of the form |
A String, representing the name of the form.
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:
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: