Jump to content

Jquery toggle hidden - help


leafer

Recommended Posts

I've been at this on and off for a week but it never seems to work quite right.

 

Basically I have multiple divs that are hidden preceded by a button before each one but it has to start hidden on load. All I want is any of the buttons to only open the div directly after it. At the moment it seems to want to open all of them.

 

Here is the snippet of code:

 

<div class="buttonClass"><button class="ForumButton" type="button">Click to open</button></div><div class="forumPaster">Hidden information</div>

 

Here's what I have so far:

 

$(document).ready(function()  {

$(".forumPaster").hide();

$(".ForumPasteButton:button").bind('click', function() {

$(".forumPaster").toggle();

});

});

 

 

 

Link to comment
Share on other sites

Hi,

It is opening all the hidden elements because that is what you are telling it to do.

In the same way that you have used $('.forumPaster').hide(); to hide them all, you are using the same code when you want to open just one.

Either you need to address each hidden layer individually by using a unique id, or you could try to get the "next" layer after the button.

 

I am not familar with jquery, personally preferring mootools, but you could try something like this:

$(document).ready(function()  {
  $(".forumPaster").hide();
  $(".ForumPasteButton:button").bind('click', function() {
    this.next(".forumPaster'").toggle();
  });
});

As I say, I don't know jquery code that well but this, or an adaption of this should work.

 

Chris

Link to comment
Share on other sites

I even tried something like this:

 

$(document).ready(function()  {

$(".forumPaster").hide();

$(".ForumPasteButton:button").bind('click', function() {

$(".forumPaster").each(function(){

$(this).toggle();

});

});

});

 

Nothing. I can't seem to find anything that says only the next one only.

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.