Javascript Reference - HTML DOM Form target Property








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

The target attribute sets where to display the response after submitting the form.

Browser Support

The target property is supported in all major browsers.

target Yes Yes Yes Yes Yes

Syntax

Return the target property.

var v = formObject.target

Set the target property.

formObject.target='_blank|_self|_parent|_top|framename'




Property Values

Value Description
_blank Show the response in a new window
_self Show the response in the same frame as it was clicked. This is the default value.
_parent Show the response in the parent frameset
_top Show the response in the full body of the window
framename Show the response in a named frame

Return Value

A String type value representing where to show the response after submitting the form.





Example

The following code shows how to get the target attribute in a form.


<!DOCTYPE html>
<html>
<body>
<!--from   w  w w. j  a  va2 s.com-->
<form id="myForm" action="url" target="_self">
  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").target;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows:

Example 2

The following code shows how to change where to show the response after submitting a form.


<!DOCTYPE html>
<html>
<body>
<!--  ww  w.  ja v a 2 s .co  m-->
<form id="myForm" 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").target = "_blank";
}
</script>

</body>
</html>

The code above is rendered as follows: