How to get the collection of all <a> elements in a document and print the value of the href attribute in JavaScript

2 Answers

0 votes
<!DOCTYPE html>        
<html>
<head>
<title></title>
</head>
<body>

<a href="aa.com">aa</a>
<a href="bb.com">bb</a>
<a href="cc.com">cc</a>
<br />
<script type="text/JavaScript">   

var links = document.links;
for(var i = 0; i < links.length; i++) 
{
    var href = document.createTextNode(links[i].href);
    document.write(href.textContent + "<br />");
}


/*
run:

http://localhost:8080/aa.com
http://localhost:8080/bb.com
http://localhost:8080/cc.com

*/

</script>
</body>
</html>

 



answered Jun 10, 2016 by avibootz
0 votes
<!DOCTYPE html>        
<html>
<head>
<title></title>
</head>
<body>

<a href="aa.com">aa</a>
<a href="bb.com">bb</a>
<a href="cc.com">cc</a>
<br />
<script type="text/JavaScript">   

var links = document.links;
for(var i = 0; i < links.length; i++) 
{
    var hrefText = document.createTextNode(links[i].href);
    var lineBreak = document.createElement("br");
    document.body.appendChild(hrefText);
    document.body.appendChild(lineBreak);
}


/*
run:

http://localhost:8080/aa.com
http://localhost:8080/bb.com
http://localhost:8080/cc.com

*/

</script>
</body>
</html>

 



answered Jun 10, 2016 by avibootz
...