-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcard.js
53 lines (40 loc) · 1.21 KB
/
card.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
44
45
46
47
48
49
50
51
52
53
//Funksjoner for å legge til cards
//legger til cards i parent
function addNewCard(parent, array, options = {}) {
/**
parent: Htmlelement, hvor den skal legge til de nye cardene
array: {
img: bilde src,
html: card-content,
index: number, setter data-index attribute
} hva den skal legge til i card-elementene
options
click: function, kjører ved klikk
*/
array.forEach(elem => {
const div = document.createElement("div");
div.className = "card";
if (elem.img) {
const img = document.createElement("div");
img.className = "card-img";
const im = document.createElement("img");
im.src = elem.img;
img.appendChild(im);
div.appendChild(img);
}
if (elem.html) {
const content = document.createElement("div");
content.className = "card-content";
content.innerHTML = elem.html;
div.appendChild(content);
}
if (typeof elem.index === "number") {
div.setAttribute("data-index", elem.index);
}
if (options.click) {
div.style.cursor = "pointer";
div.addEventListener("click", options.click);
}
parent.appendChild(div);
});
}