Open In App
Related Articles

HTML | DOM Form target Property

Improve Article
Improve
Save Article
Save
Like Article
Like

The DOM Form Target property is used to set or return the value of the target attribute of the form.The Target attribute is used to specify whether the submitted result will open in the current window, a new tab or on a new frame.

Syntax:

  • It is used to return the target property.
    formObject.target
  • It is used to set the target property.
    formObject.target = "_blank|_self|_parent|_top|framename"
  • Property Values

    • _blank: It defines that submitted result will open in a new window or in a new tab.
    • _self: It is the default value. It specify that the submitted result will be open in a same window.
    • _parent: It specify that the result will be open in a parent frameset.
    • _top: It specify that the result will open in a full body of the window.
    • framename: It opens in a named frame.

    Return Value: It returns a string value which represents that whether the submitted result will open in the current window, a new tab or on a new frame.

    Example-1: HTML Program that illustrate how to set the Property.




    <!DOCTYPE html>
    <html>
      
    <head>
        <title>DOM Form target Property</title>
        <style>
            h1 {
                color: green;
            }
              
            body {
                text-align: center;
            }
        </style>
    </head>
      
    <body>
        <h1>GeeksforGeeks</h1>
        <h2>DOM Form target Property</h2>
        <form action="#" id="GFG" target="_self">
            First name:
            <input type="text" name="fname">
            <br> Last name:
            <input type="text" name="lname">
            <br> Address:
            <input type="text" name="Address">
            <br>
            <input type="submit" value="Submit">
        </form>
        <BR>
        <b><p>Click on Try it Button to set the
          value of the target attribute.</p>     
            
            <button onclick="Geeks()">Try it</button>
            <p id="sudo" 
               style="color:green;fonnt-size:20px;">
          </p>
            
            <script>
                function Geeks()  {
              // Set _blank property.
            var x = document.getElementById(
              "GFG").target = "_blank";
           document.getElementById("sudo").innerHTML = 
             "The value of the target attribute was changed to" 
           + x;
                }
            </script>
        </body>
    </html>

    
    

    Output:

    Before Clicking On Button:

    After Clicking On Button:

    Example-2: HTML Program that illustrate how to return the property.




    <!DOCTYPE html>
    <html>
      
    <head>
        <title>DOM Form target Property</title>
        <style>
            h1 {
                color: green;
            }
              
            body {
                text-align: center;
            }
        </style>
    </head>
      
    <body>
        <h1>GeeksforGeeks</h1>
        <h2>DOM Form target Property</h2>
        <form action="#" 
              id="GFG" 
              target="_self">
            First name:
            
            <input type="text" 
                   name="fname">
            <br> Last name:
            
            <input type="text" 
                   name="lname">
            <br> Address:
            
            <input type="text" 
                   name="Address">
            <br>
            
            <input type="submit" 
                   value="Submit">
        </form>
        <BR>
        
      <b><p>Click on Try it Button to return 
        the value of the target attribute.</p></b>
        
            <button onclick="Geeks()">Try it</button>
            <p id="sudo" style="color:green;font-size:30px;">
      </p>
            <script>
                function Geeks()  {
                    
                // Return form target property.
            var x = document.getElementById("GFG").target;
           document.getElementById("sudo").innerHTML = x;
                }
            </script>
        </body
    </html>

    
    

    Output:

    Before Clicking On Button:

    After Clicking On Button:

    Supported Browsers:

    • Google Chrome 1 and above
    • Edge 12 and above
    • Firefox 1 and above
    • Opera 12.1 and above
    • Safari 3 and above

    Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

    Last Updated : 31 Aug, 2022
    Like Article
    Save Article
    Previous
    Next
Similar Reads
Complete Tutorials