Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

94
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda