Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

217
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda