EN
JavaScript - get link text (or content)
0 points
In this article, we would like to show you how to get link text using JavaScript.
Quick solution:
xxxxxxxxxx
1
<a id="link" href="https://some-domain.com">Some domain</a>
2
<script>
3
4
var link = document.querySelector('#link');
5
6
console.log(link.text); // available only in Anchor HTML Element (<a> element)
7
console.log(link.innerText); // available for each HTML Element (Element extends Node)
8
console.log(link.innerHTML); // available for each HTML Element (Element extends Node)
9
console.log(link.textContent); // available for each Node
10
11
</script>
In this example, we use four different methods to get the text from the a
element.
xxxxxxxxxx
1
2
<html>
3
<body>
4
<a id="link" href="https://some-domain.com">Some domain</a>
5
<script>
6
7
var link = document.querySelector('#link');
8
9
console.log(link.text); // available only in Anchor HTML Element (<a> element)
10
console.log(link.innerText); // available for each HTML Element (Element extends Node)
11
console.log(link.innerHTML); // available for each HTML Element (Element extends Node)
12
console.log(link.textContent); // available for each Node
13
14
</script>
15
</body>
16
</html>
Output:
xxxxxxxxxx
1
Some domain
2
Some domain
3
Some domain
4
Some domain