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

114
Visualizações
Creating a Dynamic List with a href using JS on HTML

Can someone help me create a Dynamic List with Href.. I only know how to add a list but I don't know how to add links for every list

<h1>Toppings</h1>
<ul>
 
</ul>
<script>
var toppings = ['tomatoes', 'olives', 'cheese', 'peperonies']
</script>

Now i want to have a dynamic list with links not just list. Can someone help me.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You need to create an anchor tag inside a list item for each topping and append it to your unordered list

<h1>
    Toppings
</h1>
<ul id="toppings-list">

</ul>
<script>
    let toppings = ['tomatoes', 'olives', 'cheese', 'peperonies'];
    const toppingsList = document.querySelector('#toppings-list');

    toppings.forEach(topping => {
        const listItem = document.createElement('li');
        const link = document.createElement('a');
        link.setAttribute('href', topping);
        link.innerText = topping;

        listItem.appendChild(link);
        toppingsList.appendChild(listItem);
    
    });
</script>

about 4 years ago · Juan Pablo Isaza Relatório

0

The traditional way is with the .createElement and .appendChild methods.

Edit:
Inserting links depends on where you want the links to go (of course) but the snippet now includes a crude demonstration of dynamically creating anchor elements related to each list item.

const toppings = ['tomatoes', 'olives', 'cheese'];
const myUl = document.getElementById("my-ul");

for(let topping of toppings){
  const newLi = document.createElement("li");
  const newAnchor = document.createElement("a");
  newAnchor.href = "#" + topping;
  newAnchor.textContent = topping;
  newLi.appendChild(newAnchor);  
  myUl.appendChild(newLi);
}
<h1>Toppings</h1>
<ul id="my-ul"></ul>

<div>-</div><div>-</div><div>-</div>
<div id="tomatoes">tomatoes target</div>

<div>-</div><div>-</div><div>-</div>
<div id="olives">olives target</div>

<div>-</div><div>-</div><div>-</div>
<div id="cheese">cheese target</div>

<div>-</div><div>-</div><div>-</div>
<div>-</div><div>-</div><div>-</div>
<div>-</div><div>-</div><div>-</div>
<div>-</div><div>-</div><div>-</div>
<div>-</div><div>-</div><div>-</div>

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