Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-23-2009, 02:33 PM   PM User | #1
johnf
New to the CF scene

 
Join Date: Oct 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
johnf is an unknown quantity at this point
each() not working out for me

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:

Code:
$(document).ready(function()  {
   $(".forumPaster").hide();
   $(".ForumPasteButton:button").bind('click', function() {
         $(".forumPaster").each(function(){
         $(this).toggle();
         });   
      });
});
Nothing. It still wants all of them. I can't seem to find anything that says only the next one only.
johnf is offline   Reply With Quote
Old 10-23-2009, 03:00 PM   PM User | #2
tomws
Senior Coder

 
tomws's Avatar
 
Join Date: Nov 2007
Location: Arkansas
Posts: 2,644
Thanks: 29
Thanked 330 Times in 326 Posts
tomws will become famous soon enoughtomws will become famous soon enough
The each() function applies the request to each matched item. So, in your case, it's going to do just what you're getting: apply a toggle to each item of class forumPaster - that's all of them. Just what it says.

To further confuse the issue, you're binding the click function on every button to do the same thing. So you're doubly telling it to annoy and confound you to no end.

I'm far from a jquery pro, so with the little knowledge that I do have, I'd try a couple of things. First, if your layout implies a parent/child/sibling structure between the buttons and their supposed associated divs, use jquery selectors to narrow down the toggle action.

If the layout doesn't allow for non-trivial traversal like that, though, you could do it easily by applying IDs to every element and setting up your button/click/toggle combo for each ID. More code, but easy to target exactly what you want.
__________________
Are you a Help Vampire?
tomws is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:53 AM.


Advertisement
Log in to turn off these ads.