Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Add link text to span class name

I have a list of links. And I want that link texts inserts as span span data-hover names. Please tell me how can i do this. Thanks!

What i have:

<a href="#">Paris</a>
<a href="#">London</a>
<a href="#">Berlin</a>

What i want:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<a href="#"><span data-hover="Paris">Paris</span></a>
<a href="#"><span data-hover="London">London</span></a>
<a href="#"><span data-hover="Berlin">Berlin</span></a>

>Solution :

The below code will work –

var links = document.getElementsByTagName("a");
for (i = 0; i < links.length; i++) {
  links[i].innerHTML = '<span data-hover="' + links[i].innerHTML + '">' + links[i].innerHTML + '</span>'
}
<a href="#">Paris</a>
<a href="#">London</a>
<a href="#">Berlin</a>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading