Javascript Reference - HTML DOM Anchor search Property








The search property sets or gets the querystring part of the href attribute value.

The querystring used for parameter passing is the part of the URL after the question mark (?).

Browser Support

search Yes Yes Yes Yes Yes

Syntax

Return the search property:

var v = anchorObject.search;

Set the search property:

anchorObject.search = querystring;




Property Values

Value Description
querystring Set the query string

Return Value

A String representing the querystring part of the URL.

Example

The following code shows how to get the querystring part of a link.


<!DOCTYPE html>
<html>
<body>
<!--from  w  w w  . j  av a  2s.  c om-->
<p><a id="myAnchor" href="http://www.example.com:80/test.htm?id=myPara">Example link</a></p>
<button onclick="myFunction()">test</button>

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

<script>
function myFunction() {
    var x = document.getElementById("myAnchor").search;
    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 querystring part of a link.


<!DOCTYPE html>
<html>
<body>
<!--   w  w w  . j  ava 2  s .  c o m-->
<p><a id="myAnchor" target="_blank" href="http://www.example.com:80/test.htm?id=myPara">Example link</a></p>
<button onclick="myFunction()">test</button>

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

<script>
function myFunction() {
    document.getElementById("myAnchor").search = "newValue";
    document.getElementById("demo").innerHTML = "changed";
}
</script>

</body>
</html>

The code above is rendered as follows: