-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
43 lines (30 loc) · 1.22 KB
/
scripts.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
window.addEventListener("load", function(){
console.log("links/scripts.js loaded");
// newly-created link row
var createdLink;
// container where the links will go
var linksContainer = document.querySelector("[data-links-container]");
// HTML link template
var itemTemplate = document.getElementById("link-template");
var itemTemplateElem = itemTemplate.content.querySelector("[data-link-row]");
// get JSON data
fetch("links.json")
.then(response => response.json())
.then(data => {
for(let x = 0; x < data.linkItems.length; x++){
// get HTML template from document to duplicate for each link
createdLink = document.importNode(itemTemplateElem, true);
// set innerHTML of label
createdLink.querySelector("[data-link-label]").innerHTML = data.linkItems[x].label;
// set href of a tag
createdLink.href = data.linkItems[x].href;
// add additional item class (optional)
if(data.linkItems[x].class != ""){
createdLink.classList.add(data.linkItems[x].class);
}
// append to link container
linksContainer.appendChild(createdLink);
}
})
///////////////
}, false);