As a javascript newbie, I need some help. I'm building html pages that have certain multiple elements (in my case, different translations of the bible) switching in or out based upon a user's preferences. I need to change the display property of a given css class between "display:block" and "display:none" with a button click. My current code works for changing the first instance of a paragraph in a css class, but what I want is to be able to change the display properties of ALL instances of a css with a button click. My research tells me I probably need a "for" loop in my javascript, but I haven't been able to figure out the proper syntax for how to write and implement that so that the button clicks will show/hide ALL instances of the same css class. Here's my code:
<style>
p.niv {display:block;}
p.esv {display:none;}
p.kjv {display:none;}
</style>
<p>Click the buttons to display your preferred Bible version:</p>
<button onclick="niv()">NIV</button>
<button onclick="kjv()">KJV</button>
<button onclick="esv()">ESV</button>
<p>Regular paragraph, blah blah</p>
<p class="niv">this NIV paragraph will become hidden when the KJV or ESV button is clicked</p>
<p class="niv">but this NIV paragraph stays visible even though I want it to be hidden too</p>
<p class="kjv">this KJV paragraph will become hidden when the NIV or ESV button is clicked</p>
<p class="kjv">but this KJV paragraph stays visible even though I want it to be hidden too</p>
<p class="esv">this ESV paragraph will become hidden when the KJV or NIV button is clicked</p>
<p class="esv">but this ESV paragraph stays visible even though I want it to be hidden too</p>
<script>
var n = document.getElementsByClassName("niv") [0];
var k = document.getElementsByClassName("kjv") [0] ;
var e = document.getElementsByClassName("esv") [0];
function niv() {
n.style.display = "block";
k.style.display = "none";
e.style.display = "none";
}
function kjv() {
n.style.display = "none";
k.style.display = "block";
e.style.display = "none";
}
function esv() {
n.style.display = "none";
k.style.display = "none";
e.style.display = "block";
}
</script>
How can I loop my javascript functions to change the display properties of every instance of a css class? Thanks in advance for helping out a newbie!