Javascript Reference - HTML DOM Anchor text Property








The text property sets or gets the text content of a link.

Browser Support

text Yes Yes Yes Yes Yes

Syntax

Return the text property:

var v = anchorObject.text 

Set the text property:

anchorObject.text = sometext;

Property Values

Value Description
sometext Set the link text




Return Value

A String representing the text content of the link.

Example

The following code shows how to get the text content of a link.


<!DOCTYPE html>
<html>
<body>
<!--from   ww w. j a v  a  2  s . com-->
<p><a id="myAnchor" href="http://www.example.com/test.htm#part2">Example link</a></p>
<button onclick="myFunction()">test</button>

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

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


<!DOCTYPE html>
<html>
<body>
<!--from w ww. ja va  2s. c o m-->
<p><a id="myAnchor" href="http://www.example.com/test.htm#part2">Example link</a></p>
<button onclick="myFunction()">test</button>

<script>
function myFunction() {
    document.getElementById("myAnchor").text = "changed";
}
</script>

</body>
</html>

The code above is rendered as follows: