Computer >> Computer tutorials >  >> Programming >> Javascript

How to find the name and the target of a form in JavaScript?


A form generally has a name and a target. Without a name, it is very difficult to validate any form. To find the name and target of a form, javascript has provided document.name and document.target respectively. Let's discuss them individually.

Name of a Form

Example

In the following example, the name of the form is found out using document.name method of javascript. The name of the form is used to validate a form.

<html>
<body>
   <form id="form1" name="form" action="/action_page.php"></form>
<p id="name"></p>
<script>
   var nm = document.getElementById("form1").name;
   document.getElementById("name").innerHTML = nm;
</script>
</body>
</html>

Output

form


Target of a form

Example

In the following example, the target of form is found out using document.target method of javascript.

<html>
<body>
   <form id="form1" target="_self" action="/action_page.php"></form>
<p id="target"></p>
<script>
   var tar = document.getElementById("form1").target;
   document.getElementById("target").innerHTML = tar;
</script>
</body>
</html>

Output

_self