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

How to get all a tag innerHTML in a span class

<span class="mgen">
<a rel="tag">Action</a> 
<a rel="tag">Adventure</a> 
<a rel="tag">Apocalypse</a> 
<a rel="tag">Fantasy</a> 
<a rel="tag">Magic</a> 
</span>

How do I get all the a tag inner html like

Action Adventure Apocalypse Fantsay Magic

I tried using document.querySelector(".mgen a").innerHTML
But I am only getting first a tag innerHTML and I also don’t want to use loop to get all innerHTML as it would be lengthy any way anyone can suggest me?

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

>Solution :

We can try getting the textContent of the .mgen element. This would return only the text content of the inner elements.

console.log(document.querySelector(".mgen").textContent)
<span class="mgen">
<a rel="tag">Action</a> 
<a rel="tag">Adventure</a> 
<a rel="tag">Apocalypse</a> 
<a rel="tag">Fantasy</a> 
<a rel="tag">Magic</a> 
</span>

If you want it in the same line with only one space between the words, we can remove the newline and other spacing using regex and replace them with space

console.log(document.querySelector(".mgen").textContent.replace(/\s/g, ' '))
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