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

113
Views
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 answers
Answer question

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 Report

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 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!