Javascript Reference - HTML DOM Button formTarget Property








The formtarget attribute ,new for the <button> element in HTML5, set where to display the response after submitting the form.

This attribute overrides the form's target attribute.

The formtarget attribute is only used for buttons with type="submit".

The formTarget property sets or gets the value of the formtarget attribute of a button.

Browser Support

formTarget Yes Yes Yes Yes Yes

Syntax

Return the formTarget property:

var v = buttonObject.formTarget 

Set the formTarget property:

buttonObject.formTarget='_blank|_self|_parent|_top|framename'




Property Values

Value Description
_blank Loads the response in a new window or tab
_self Loads the response in the same frame. Tthis is default
_parent Loads the response in the parent frame
_top Loads the response in the full body of the window
framename Loads the response in a named iframe

Return Value

A String representing where to display the response after submitting the form.





Example

The following code shows how to get where to display the response after submitting a form:


<!DOCTYPE html>
<html>
<body>
<!--from  w w  w .jav a 2 s. c o m-->
<form action="url" method="get" target="_self">
First name: <input type="text" name="fname" value="A"><br>
Last name: <input type="text" name="lname" value="D"><br>
<button id="myBtn" type="submit" formtarget="_blank">Submit to a new window/tab</button>
</form>

<button onclick="myFunction()">test</button>

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

<script>
function myFunction() {
    var x = document.getElementById("myBtn").formTarget;
    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 value of the formtarget attribute of a button.


<!DOCTYPE html>
<html>
<body>
<form action="url" method="get">
First name: <input type="text" name="fname" value="A"><br>
Last name: <input type="text" name="lname" value="D"><br>
<button id="myBtn" type="submit" formtarget="_blank">Submit</button>
</form><!--from   w  w w  . j  av  a  2 s .  c om-->

<button onclick="myFunction()">test</button>

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

<script>
function myFunction() {
    document.getElementById("myBtn").formTarget = "_self";
    document.getElementById("demo").innerHTML = "changed";
}
</script>
</body>
</html>

The code above is rendered as follows: