What is the best way to create links between dynamically-generated HTML content with JavaScript?
For example, I have a number of thumbnail images on a homepage that should open the matching project when clicked. I’ve come up with this solution but I’m sure there’s a better way that doesn’t involve using split() to select the correct class?
The order of the thumbnails and projects will be randomised so I can’t use the index of the thumbnail to open the project page with the same index.
https://codepen.io/wrgt1/pen/OJRwNQv
JavaScript
x
18
18
1
const thumbnail = document.querySelectorAll(".thumbnail");
2
const project = document.querySelectorAll(".project");
3
4
thumbnail.forEach(function (thumb) {
5
thumb.addEventListener("click", (e) => {
6
7
const splitClass = e.target.className.split(" ")[1];
8
const target = `.${splitClass}:not(.thumbnail)`;
9
const targetSelector = document.querySelector(target);
10
11
for (let i = 0; i < project.length; i++) {
12
project[i].style.visibility = "hidden";
13
}
14
15
targetSelector.style.visibility = "visible";
16
17
});
18
});
JavaScript
1
20
20
1
#thumbnails, #projects {
2
position: relative;
3
display: flex;
4
}
5
6
.thumbnail, .project {
7
height: 100px;
8
width: 100px;
9
margin: 10px;
10
}
11
12
.thumbnail {
13
background: #FF7400;
14
cursor: pointer;
15
}
16
17
.project {
18
visibility: hidden;
19
background: #209209;
20
}
JavaScript
1
10
10
1
<div id="thumbnails">
2
<div class="thumbnail project1">Thumbnail (Project 1)</div>
3
<div class="thumbnail project2">Thumbnail (Project 2)</div>
4
<div class="thumbnail project3">Thumbnail (Project 3)</div>
5
</div>
6
<div id="projects">
7
<div class="project project1">Project 1</div>
8
<div class="project project2">Project 2</div>
9
<div class="project project3">Project 3</div>
10
</div>
I’d be really grateful for any ideas!
Advertisement
Answer
Don’t see anything wrong with your code, I have also tried it in some different way
Hope you take a look at the same also
JavaScript
1
14
14
1
const thumbnail = document.querySelectorAll(".thumbnail");
2
const project = document.querySelectorAll(".project");
3
4
thumbnail.forEach(function (thumb) {
5
thumb.addEventListener("click", (e) => {
6
const target = e.target.classList[1];
7
project.forEach(function (pro) {
8
pro.style.visibility = "hidden";
9
if( pro.classList.contains(target)){
10
pro.style.visibility = "visible";
11
}
12
});
13
});
14
});
JavaScript
1
20
20
1
#thumbnails, #projects {
2
position: relative;
3
display: flex;
4
}
5
6
.thumbnail, .project {
7
height: 100px;
8
width: 100px;
9
margin: 10px;
10
}
11
12
.thumbnail {
13
background: #FF7400;
14
cursor: pointer;
15
}
16
17
.project {
18
visibility: hidden;
19
background: #209209;
20
}
JavaScript
1
10
10
1
<div id="thumbnails">
2
<div class="thumbnail project1">Thumbnail (Project 1)</div>
3
<div class="thumbnail project2">Thumbnail (Project 2)</div>
4
<div class="thumbnail project3">Thumbnail (Project 3)</div>
5
</div>
6
<div id="projects">
7
<div class="project project1">Project 1</div>
8
<div class="project project2">Project 2</div>
9
<div class="project project3">Project 3</div>
10
</div>