javascriptで同じ処理をまとめるにはどのようにすればいいでしょうか。
const but = document.getElementById("but");
const trend = document.getElementById("trend");
but.addEventListener("click", function () {
if (trend.style.visibility == "visible") {
// hiddenで非表示
trend.style.visibility = "hidden";
} else {
// visibleで表示
trend.style.visibility = "visible";
}
const but2 = document.getElementById("but2");
const trend2 = document.getElementById("trend2");
//初期表示は非表示
trend2.style.visibility = "hidden";
but2.addEventListener("click", function () {
if (trend2.style.visibility == "visible") {
// hiddenで非表示
trend2.style.visibility = "hidden";
} else {
// visibleで表示
trend2.style.visibility = "visible";
}
});
const but3 = document.getElementById("but3");
const trend3 = document.getElementById("trend3");
//初期表示は非表示
trend3.style.visibility = "hidden";
but3.addEventListener("click", function () {
if (trend3.style.visibility == "visible") {
// hiddenで非表示
trend3.style.visibility = "hidden";
} else {
// visibleで表示
trend3.style.visibility = "visible";
}
});