how to write/print href links with link text/title - javascript

235 Views Asked by At

this is my code - not complete.

how to write/print href links with link text/title. result like this http://google.com/123.asp 123 http://google.com/456.asp 456 http://google.com/789.asp 789

<y id='txt'>
<a href="http://google.com/123.asp">123</a>
<a href="http://google.com/456.asp">456</a>
<a href="http://google.com/789.asp">789</a>
</y>


<script>

    var y = document.getElementById("txt");
   var linktext = p.getElementsByTagName("textcontent");
   const links = document.links;
   var text = '';
   for (var i = 0; i < linktext.length; i++){
       text += links[i] + "<br>" + linktext[i].innerHTML + "<br>";
   }
    
   p.innerHTML = text;



</script>
1

There are 1 best solutions below

0
On BEST ANSWER

You can try this

`

<script>    
var y = document.getElementById("txt");
var links = y.getElementsByTagName('a')
var text = '<p>';
for (var i = 0; i < links.length; i++){
   text += '<a href="' + links[i].getAttribute('href') + '">' + links[i].getAttribute('href') +  '</a>'  + " " + links[i].innerText + "<br/>";
}

text+= '</p>'

y.innerHTML = text;

</script>

`