Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,895 questions

51,826 answers

573 users

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
...