brooksh Posted April 26, 2013 Share Posted April 26, 2013 I want to show and hide all of them at once no matter which button I click on. I'm sure there is an easy way, but I don't know javascript. <script type="text/javascript"> function showDiv() { document.getElementById('welcomeDiv').style.display = "block"; } function showDiv2() { document.getElementById('welcomeDiv2').style.display = "none"; } </script> <div id="welcomeDiv" style="display:none;" class="answer_list" > WELCOME</div> <div id="welcomeDiv2" style="display:block;" class="answer_list" > <input type="button" name="answer" value="Show Div" onclick="showDiv();showDiv2();" /></div> <div id="welcomeDiv" style="display:none;" class="answer_list" > WELCOME2</div> <div id="welcomeDiv2" style="display:block;" class="answer_list" > <input type="button" name="answer" value="Show Div" onclick="showDiv();showDiv2();" /></div> <div id="welcomeDiv" style="display:none;" class="answer_list" > WELCOME3</div> <div id="welcomeDiv2" style="display:block;" class="answer_list" > <input type="button" name="answer" value="Show Div" onclick="showDiv();showDiv2();" /></div> Quote Link to comment Share on other sites More sharing options...
Solution Psycho Posted April 26, 2013 Solution Share Posted April 26, 2013 Elements cannot have the same ID. Not sure if this is really the best solution for you since I don['t know what you are really trying to achieve, but . . <script type="text/javascript"> function showDivs(displayBool) { var divObjs = document.getElementsByClassName('answer_list'); var divLength = divObjs.length; for(var i=0; i<divLength; i++) { divObjs[i].style.display = (divObjs[i].style.display=='none') ? 'block' : 'none'; } } </script> <div id="welcomeDiv1" style="display:none;" class="answer_list" > WELCOME</div> <div id="welcomeDiv2" style="display:block;" class="answer_list" > <input type="button" name="answer" value="Show Div" onclick="showDivs();" /></div> <div id="welcomeDiv3" style="display:none;" class="answer_list" > WELCOME2</div> <div id="welcomeDiv4" style="display:block;" class="answer_list" > <input type="button" name="answer" value="Show Div" onclick="showDivs();" /></div> <div id="welcomeDiv5" style="display:none;" class="answer_list" > WELCOME3</div> <div id="welcomeDiv6" style="display:block;" class="answer_list" > <input type="button" name="answer" value="Show Div" onclick="showDivs();" /></div> Quote Link to comment Share on other sites More sharing options...
brooksh Posted April 26, 2013 Author Share Posted April 26, 2013 Great. Thanks. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.