<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?
>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, ' '))