r/javaScriptStudyGroup • u/fragrantmoon • Feb 03 '22
Is there a more efficient way to call the same event handler for multiple DOM elements
Hello, apologies for the code snippet its not complicated just very repetitive
I included some images of my mock up portfolio page and tried my best to explain what im trying to achieve. Im trying to make an section that displays an image with the projects title in the middle, and 4 buttons. When clicked it will show a paragraph & hide the title, when you move the mouse it will switch back.
(1) My questions are.. is there a more efficient way to implement this rather than having to assign variables to each element and calling onclick/onmouseout event listeners for each variable. If I wanted to add more sections like this it would be very tedious.
(2) At first the paragraph wouldn't show unless unless I press a button twice the first time round. I found that calling the function at least once for each button fixed this, but I don't understand why.
(3) I also found it strange how I could not get this to work unless I passed the function inside an anonymous function instead of directly assigning the function like so:
button1.onclick = togggleParagraph(heading, paragraph1) why is this?
<ul>
<li id = "challenges1">Challenges</li>
<li id = "solutions1">Solutions</li>
<li id = "features1">Features</li>
<li id = "tech1">Technologies</li>
</ul>
<figure>
<img id = "project1" src="resources/images/project2.jpg">
<h2 id = "one">Martial Arts Club</h2>
<p id = "p1.1">Lorem ipsum ....</p>
<p id = "p1.2">Lorem ipsom ....</p>
<p id = "p1.3">Lorem ipsom ....</p>
<p id = "p1.4">Lorem ipsum ....</p>
</figure>
<script>
/* Assigning elements to variables */
let heading = document.getElementById('one');
let button1 = document.getElementById('challenges1');
let paragraph1 = document.getElementById('p1.1');
let button2 = document.getElementById('solutions1');
let paragraph2 = document.getElementById('p1.2')
let button3 = document.getElementById('features1');
let paragraph3 = document.getElementById('p1.3');
let button4 = document.getElementById('tech1');
let paragraph4 = document.getElementById('p1.4');
/* If the title is showing and paragraph is hidden,
show the paragraph and hide the title. Otherwise show
the title and hide the paragraph. */
function toggleParagraph(heading, paragraph) {
if (paragraph.style.display === 'none' &&
heading.style.display === 'block') {
heading.style.display = 'none';
paragraph.style.display = 'block';
} else {
heading.style.display = 'block';
paragraph.style.display = 'none';
}
}
/* If the paragraph is showing and title is
hidden, show the title and hide the paragraph. */
function hideParagraph(heading, paragraph) {
if (paragraph.style.display === 'block' &&
heading.style.display === 'none') {
paragraph.style.display = 'none';
heading.style.display = 'block';
}
}
/* Button requires 2 clicks to toggle paragraph.
One solution was to call the function once before
assigning it to event listeners */
toggleParagraph(heading, paragraph1);
toggleParagraph(heading, paragraph2);
toggleParagraph(heading, paragraph3);
toggleParagraph(heading, paragraph4);
button1.onclick = function () {
toggleParagraph(heading, paragraph1)
};
button2.onclick = function () {
toggleParagraph(heading, paragraph2)
};
button3.onclick = function () {
toggleParagraph(heading, paragraph3)
};
button4.onclick = function () {
toggleParagraph(heading, paragraph4)
};
button1.onmouseout = function () {
hideParagraph(heading, paragraph1)
};
button2.onmouseout = function () {
hideParagraph(heading, paragraph2)
};
button3.onmouseout = function () {
hideParagraph(heading, paragraph3)
};
button4.onmouseout = function () {
hideParagraph(heading, paragraph4)
};
</script>