Open In App

HTML DOM Anchor search Property

Last Updated : 29 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The Anchor search Property in HTML DOM is used to set or return the querystring part of the href attribute. The querystring part is specified after the question mark(?) in the URL. It is often used for parameter passing.

Syntax:

  • It returns the Anchor search property.
    anchorObject.search
  • It is used to set the Anchor search property.
    anchorObject.search = querystring 

Property Value: It contains single value querystring which is used to specify the search part of the URL.

Return Value: It returns a string value which represents the query string part of the URL also included the question mark (?).

Example 1: This examples returns the Anchor search Property.

html




<!DOCTYPE html>
<html>
 
<head>
    <title>
        HTML DOM Anchor search Property
    </title>
</head>
 
<body style="text-align: center;">
 
    <h1>GeeksforGeeks</h1>
 
    <h2>HTML DOM Anchor search Property</h2>
 
    <p>Welcome to
        <a href=
            id="GFG" target="_self">
            GeeksforGeeks
        </a>
    </p>
 
    <button onclick="myGeeks()">
        Click Here!
    </button>
 
    <p id="result"></p>
 
    <script>
        function myGeeks() {
            let x = document.getElementById("GFG").search;
            document.getElementById("result").innerHTML = x;
        }
    </script>
</body>
 
</html>


Output:

anchor-search

Example 2: This example sets the Anchor search property.

html




<!DOCTYPE html>
<html>
 
<head>
    <title>
        HTML DOM Anchor search Property
    </title>
</head>
 
<body style="text-align: center;">
 
    <h1>GeeksforGeeks</h1>
 
    <h2>HTML DOM Anchor search Property</h2>
 
    <p>Welcome to
        <a href=
            id="GFG" target="_self">
            GeeksforGeeks
        </a>
    </p>
 
    <button onclick="myGeeks()">
        Click Here!
    </button>
 
    <p id="result"></p>
 
    <!-- Script to set Anchor search Property -->
    <script>
        function myGeeks() {
            let x = document.getElementById("GFG").search
                = "post_id=987654&name=gfg";
 
            document.getElementById("result").innerHTML
                = "Anchor Search Change to " + x;
        }
    </script>
</body>
 
</html>


Output:

anchor-search-2

Supported Browsers:

  • Google Chrome 1
  • Edge 12
  • Firefox 1
  • Opera 15
  • Safari 1


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads