Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Oct 2009
    Thanked 0 Times in 0 Posts

    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:

    $(document).ready(function()  {
       $(".ForumPasteButton:button").bind('click', function() {
    Nothing. It still wants all of them. I can't seem to find anything that says only the next one only.

  2. #2
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Thanked 330 Times in 326 Posts
    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?


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts