Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

92
Views
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. I don't want to use loop to get all innerHTML as it would be lengthy.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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, ' '))
about 4 years ago · Juan Pablo Isaza Report

0

Use the .innerText property on the targeted element. .innerText() represents the rendered text content of a node and its descendants.

Try this:

<body>
    <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>
    <script>
        let element = document.querySelector(".mgen")
        console.log(element.innerText)
    </script>
</body>

You can also use textContent property. They work similar with few differences.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!