I'm relatively new to coding and JavaScript. I'm working on a basic webpage that involves showing and hiding parts of sentences for language learning purposes. Is there a way to create a single function that can show and hide the sentence when a button is clicked?
If I use document.getElementsByClassName() to get an array of buttons and iterate through them, I'm unsure how to connect them to one function that performs the showing and hiding functionality. I've seen examples using getElementById but I'm looking to achieve the same result with document.getElementsByClassName().
const a = document.getElementById("button1").addEventListener( "click" , function(){
if(document.getElementById("para1").style.visibility === "hidden"){
document.getElementById("para1").style.visibility = "visible";
}else{
document.getElementById("para1").style.visibility ="hidden";
}
I've checked other questions related to this topic, but they seem too advanced for what I'm trying to accomplish. I understand this is basic stuff, but any guidance would be greatly appreciated.
I attempted the solution above as described, but I'm struggling to figure out how to connect one function to multiple buttons.