Jump to content

Use showDiv() multiply times.


Go to solution Solved by Barand,

Recommended Posts

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 by oz11
Link to comment
https://forums.phpfreaks.com/topic/317522-use-showdiv-multiply-times/
Share on other sites

  • Solution

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>

 

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>

 

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>

:) 

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');
}

 

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>     

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.