2

I am working on creating a list of html elements using JavaScript.

Each element has an image and several lines of descriptive text, the code appears as such:

for (var i=0; i < json.length; i++){
        var section = document.createElement('section');
        var item = document.createElement('h1');
        var img = document.createElement('img');
        var datasheet = document.createElement('h3');

        item.textContent = "name: " + json[i][7];
        img.src="../cap/images/placeholder.png";
        datasheet.textContent= "../datasheet/test.pdf";


    section.appendChild(img);
    section.appendChild(item);
    section.appendChild(datasheet)

    body.append(section);
}

What tag can I use to turn the datasheet text into a link in html? I have tried DOM tags such as : 'datasheet.link=', 'datasheet.href=' neither which work.

0

1 Answer 1

2

links are anchor tags <a> so you would use document.createElement("a")

If you want the link to be h3 styling just append the link to that element, or use css

var link = document.creaetElement("a");
datasheet.appendChild(link);

var link = document.createElement("A");
link.href = "https://stackoverflow.com";
link.textContent = "Link to stackoverflow";
document.body.appendChild(link);

var linkClone = link.cloneNode(true);
var datasheet = document.createElement("h3");

datasheet.appendChild(linkClone);
document.body.appendChild(datasheet);

Sign up to request clarification or add additional context in comments.

1 Comment

Awesome! I knew it was going to be something simple. Thanks for the style info too!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.