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

225
Vistas
Create list with hyperlink (document fragments) as text string

I am totally new at javascript and am trying to create a dynamic list in JavaScript that is both a hyperlink to and shows the text string of a tag in my html code that has the id = v1. Kind of like a menu to different parts of that same webpage.

I have tried to do this using document fragments (https://developer.mozilla.org/en-US/docs/Learn/HTML/Introduction_to_HTML/Creating_hyperlinks#document_fragments) but it is not working and I don't have any idea of how to insert the text string of the in the "menu".

Here is part of my code:

var container = document.getElementById("contentarea");
var header = document.getElementById("v1");
for (i = 0; i < length.header; i++) {
        $(document.createElement("li")).li;
        $(document.createElement("a")).link.append(li)( {
            href: "#v1" 
        })

        list.appendChild(li);
        container.appendChild(list)
    }      
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You could do something like this

// div to get the textContent from, replace it with whatever you want
const divE = document.getElementById('test');
// our list tag
const list = document.getElementById('list');

// looping
for (let i = 0; i < 4; i++) {
    const listEl = document.createElement('li'); // our list element
    const aTag = document.createElement('a'); // a new <a> tag to append link 
    const linkTag = document.createElement('link'); //new link tag
    linkTag.href = '#test'; // setting hyperlink
    aTag.textContent = divE.textContent // setting the textContent for <a> tag
    aTag.appendChild(linkTag); // appending link to the a tag
    listEl.appendChild(aTag); // appending a tag to the <li> tag
    list.appendChild(listEl) // finally appending our list element to the main list
}
<div id="test">Something here</div>
<ul id="list"></ul>

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