oz11 Posted December 8, 2023 Share Posted December 8, 2023 (edited) Please could you help.. I need to make this code allow me to use the showDiv function multiple times on the page.. <script type="text/javascript"> function showDiv() { document.getElementById('welcomeDiv').style.display = "block"; } function hideDiv() { document.getElementById('welcomeDiv').style.display = "none"; } </script> <a name="answer" value="Show Div" onclick="showDiv()" >Show</a> <div id="welcomeDiv" style="display:none;" class="answer_list" > <h2>Hello</h2> <button onclick="hideDiv()" class="loginBut" style="font-family: 'Handjet', cursive; font-size: 2EM;">Close</button> </div> Thanks. It seems I need to add an index variable to the function. Don't know how though.. I'm new to JS. Edited December 8, 2023 by oz11 Quote Link to comment https://forums.phpfreaks.com/topic/317522-use-showdiv-multiply-times/ Share on other sites More sharing options...
Solution Barand Posted December 8, 2023 Solution Share Posted December 8, 2023 Such as by passing the id of the div to be shown/hidden as a function argument? function hideDiv(divid) { document.getElementById(divid).style.display = "none"; } . . . <button onclick="hideDiv('welcomeDiv')" class="loginBut" style="font-family: 'Handjet', cursive; font-size: 2EM;">Close</button> Quote Link to comment https://forums.phpfreaks.com/topic/317522-use-showdiv-multiply-times/#findComment-1613407 Share on other sites More sharing options...
oz11 Posted December 8, 2023 Author Share Posted December 8, 2023 Thanks pal. Just to finalise this was the [whole] working code: <script type="text/javascript"> function showDiv(divid) { document.getElementById(divid).style.display = "block"; } function hideDiv(divid) { document.getElementById(divid).style.display = "none"; } </script> <a name="answer" value="Show Div" onclick="showDiv('one')" >Show</a> <div id="one" style="display:none;" class="answer_list" > <h2>Hello</h2> <button onclick="hideDiv('one')" class="loginBut" style="font-family: 'Handjet', cursive; font-size: 2EM;">Close</button> </div> <a name="answer" value="Show Div 2" onclick="showDiv('two')" >Show</a> <div id="two" style="display:none;" class="answer_list" > <h2>Hello2</h2> <button onclick="hideDiv('two')" class="loginBut" style="font-family: 'Handjet', cursive; font-size: 2EM;">Close</button> </div> Quote Link to comment https://forums.phpfreaks.com/topic/317522-use-showdiv-multiply-times/#findComment-1613408 Share on other sites More sharing options...
oz11 Posted December 8, 2023 Author Share Posted December 8, 2023 Know how to get it to hide on second click? Quote Link to comment https://forums.phpfreaks.com/topic/317522-use-showdiv-multiply-times/#findComment-1613411 Share on other sites More sharing options...
Barand Posted December 8, 2023 Share Posted December 8, 2023 Inside the function, check it's display status. If hidden, show it. If not hidden, hide it. 1 Quote Link to comment https://forums.phpfreaks.com/topic/317522-use-showdiv-multiply-times/#findComment-1613412 Share on other sites More sharing options...
oz11 Posted December 8, 2023 Author Share Posted December 8, 2023 this code worked for me. . Thanks again! <script type="text/javascript"> function showDiv(divid) { var x = document.getElementById(divid); if (x.style.display === "none") { x.style.display = "block"; } else { x.style.display = "none"; } } </script> Quote Link to comment https://forums.phpfreaks.com/topic/317522-use-showdiv-multiply-times/#findComment-1613413 Share on other sites More sharing options...
kicken Posted December 8, 2023 Share Posted December 8, 2023 As a general recommendation, you could instead create a class that applies the display: none; style then use toggle. .removed { display: none; } function showDiv(divid) { document.getElementById(divid).classList.toggle('removed'); } Quote Link to comment https://forums.phpfreaks.com/topic/317522-use-showdiv-multiply-times/#findComment-1613415 Share on other sites More sharing options...
Barand Posted December 8, 2023 Share Posted December 8, 2023 Or you could use jquery toggle to do the heavy lifting for you <div style='border: 2px solid black; padding: 16px; margin-bottom: 16px;' id='target'> Hello </div> <button onclick='showhide("target")'>Show/Hide</button> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script> function showhide(id) { $("#"+id).toggle(0) } </script> Quote Link to comment https://forums.phpfreaks.com/topic/317522-use-showdiv-multiply-times/#findComment-1613416 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.