Jump to content

Show/Hide Divs with 1 button


dfowler

Recommended Posts

Hey guys, I'm not that great with Javascript so I'm hoping yall can help me.  I have created a javascript functionto show hide divs before, but it has always been multiple buttons.  Button 1 deals with Div 1 and so on.  Well with what I am doing now I have several divs with ids ranging from 1-10.  I want it so that when you click a button it will unhide the next div in line.

 

So the page starts off showing div 1, when the user clicks the button div 2 appears, so now you see div1 and div2, and so on....

 

Thanks for any help!

Link to comment
Share on other sites

Like this?

 

<script type="text/javascript">
  function toggleShow ( ele_id ) {
    var ele = document.getElementById(ele_id);
    ele.style.display = (ele.style.display == 'none') ? '' : 'none';
  }
</script>
<div id="test1">this is block 1</div>
<div id="test2">this is block 2</div>
<input type="button" value="Toggle Block 1" onclick="toggleShow('test1');" />
<input type="button" value="Toggle Block 2" onclick="toggleShow('test2');" />

Link to comment
Share on other sites

I think he wants a single button to display all the divs. Try something like this:

 

First, create some divs:

<?php
for ($i = 1; $i <= 10; $i++)
{
  echo "<div id=\"mydiv_{$i}\" style=\"display: none\">I am DIV #{$i}</div>";
}
?>

 

Now, here is the JavaScript declaration for your header that can then be tied to a button:

var nextDiv = 1;
function showNext()
{
  var id = 'mydiv_' + nextDiv;
  document.getElementById(id).style.display = 'block';
  nextDiv++; // increment for next
}

 

Now, just attach that function to your button or link:

<a href="#" onclick="showNext(); return false;">Show Next DIV</a>

 

Hope that helps.

Link to comment
Share on other sites

Like this?

 

<script type="text/javascript">
  function toggleShow ( ele_id ) {
    var ele = document.getElementById(ele_id);
    ele.style.display = (ele.style.display == 'none') ? '' : 'none';
  }
</script>
<div id="test1">this is block 1</div>
<div id="test2">this is block 2</div>
<input type="button" value="Toggle Block 1" onclick="toggleShow('test1');" />
<input type="button" value="Toggle Block 2" onclick="toggleShow('test2');" />

 

Thanks, but that is what I've done before.  What I'm looking for is only 1 button.  Here is the exact scenario.

 

You load the page and 'div1' is displaying and a button that says "Add div".  When you click this button 'div2' unhides.  You click it again 'div3' unhides, and so on.  I'm thinking some sort of counter has to be involved, but I'm not sure how to do this correctly.

Link to comment
Share on other sites

You load the page and 'div1' is displaying and a button that says "Add div".  When you click this button 'div2' unhides.  You click it again 'div3' unhides, and so on.  I'm thinking some sort of counter has to be involved, but I'm not sure how to do this correctly.

 

Did you look at my sample above? I just tested it, and it works fine. The only additional thing you'll want to check for is the max number of divs to display so you're not calling the function on non-existent DIVs once they are all showing.

Link to comment
Share on other sites

You load the page and 'div1' is displaying and a button that says "Add div".  When you click this button 'div2' unhides.  You click it again 'div3' unhides, and so on.  I'm thinking some sort of counter has to be involved, but I'm not sure how to do this correctly.

 

Did you look at my sample above? I just tested it, and it works fine. The only additional thing you'll want to check for is the max number of divs to display so you're not calling the function on non-existent DIVs once they are all showing.

Yeah sorry, I didn't even see you posted.  It works perfectly!  I modified things just a little for what I was going for.  But thanks for your help!

Link to comment
Share on other sites

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.