I have various hidden popups that are triggered to show on click using some JavaScript magic. As a beginner in JavaScript, I find myself writing multiple lines of code that look very similar.
Currently, my code for handling these popup boxes looks like this:
var paperComButton = document.getElementsByClassName('paperCompanieButton');
for (var i = 0; i < paperComButton.length; i++) {
paperComButton[i].addEventListener('click', function () {
handlePopup('paperCompanieInfo');
});
}
The next piece of code is almost identical:
var pdfPreviewButton = document.getElementsByClassName('pdfPreviewButton');
for (var i = 0; i < pdfPreviewButton.length; i++) {
pdfPreviewButton[i].addEventListener('click', function () {
handlePopup('paperJobPdf');
});
}
The issue arises when dealing with more than just these two popups, resulting in lengthy JavaScript code.
My question: Is there a way to consolidate these code snippets into a more universal solution? I hope there's a straightforward approach that can help simplify the logic for my beginner understanding!