Javascript DOM HTML Button formTarget Property set

Introduction

Change the value of the formtarget attribute of a button:

document.getElementById("myBtn").formTarget = "_self";

This example contains a form with a submit button that submits the form to a new window:

Click button to change the value of the formtarget attribute of the submit button.

View in separate window

<!DOCTYPE html>
<html>
<body>
<form action="/action_page.php" method="get">
  First name: <input type="text" name="fname" value="CSS"><br>
  Last name: <input type="text" name="lname" value="HTML"><br>
  <button id="myBtn" type="submit" formtarget="_blank">Submit</button>
</form>/*from  w  ww  .j a  va2s.  c  o m*/

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

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

<script>
function myFunction() {
  document.getElementById("myBtn").formTarget = "_self";
  document.getElementById("demo").innerHTML = "The value of the formtarget attribute was changed.";
}
</script>

</body>
</html>



PreviousNext

Related