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

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

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