tags:

views:

78

answers:

6

Can someone please tell me why this doesn't work? I'm trying to show and hide content.

Here is my markup

<div class="entry">
  <p class="posted">
    test<br />
    <a href="#" class="toggle" title="Show Comments">
      Show/Hide
    </a>
  </p>
  <div class="box" class="comment">
    test hidden comment
  </div>
</div>


$(function () {
  $('div.box').hide();
  $('a.toggle').click(function () {
    $(this).parents('.entry').next('div.box').toggle(400);
  });
});

The way this example sits right now, If I take the last closing div and move it up after the closing p tag, it works fine but shows and hides all hidden content (divs) which is not what I'm after. I'd only like to show the content that is associated with each link.

A: 

try use .parent instead of .parents.

this will work:

    $(function() {
        $('div.box').hide();
        $('a.toggle').click(function() {
        $(this).parent('.posted').next('div.box').toggle(400);
        });
    });
Anwar Chandra
Thanks Q8, I just tried it and it doesn't work. :(
Jack
element 'div.box' is not sibling to 'div.entry'. it should be 'p.posted'
Anwar Chandra
Hey Q8, yes, that did work. Thanks! One more small issue that I have no idea as to why... but when I click the test link, the browser opens the hidden content and at the same time, pushes / scrolls the content up. The same behavior as when a user click the common "Top" link to go back to the top of the page. Would you know why?
Jack
You can use .parents() as well. .parents() looks farther back into the DOM than .parent(), which only gets the immediate parent. `.parents('div.entry').children('div.box')` does the same thing.
zombat
Thanks Q8. Even though the boxes are opening just fine now, the page scrolls up when the link to the hidden content is pressed. Do you happen to know why?
Jack
is the test link/ 'top' link use 'toggle' as class? you should ask a new question. and show your markup/script for that case.
Anwar Chandra
Well, that's the thing.. I'm not using a top link but jquery needs the "#" in the a tag.
Jack
Actually, I take that back. I do have another image that will take the user to the bottom of the page where the comments box is but the page jumps up, not down.
Jack
A: 

class="comment" overrides class="box", use class="comment box"

parkim
Hi Parkim, are you referring to the issue I am having with the page jumping up to the top? I tried this but it still does the same thing.
Jack
+2  A: 

It's not the parents that's the problem, it's the next(). next() fetches sibling elements, whereas you need the nested sub-elements. Use children() instead. Try this, it worked for me:

$(function () {
  $('div.box').hide();
  $('a.toggle').click(function () {
    $(this).parents('.entry').children('div.box').toggle(400);
  });
});
zombat
Thank you Zombat. I made the changes and your solution also works. I'm also having an issue where when the link is clicked to open and close the box, the page jumps to the top. Would you know why this is?
Jack
Hi Jack, yes, it's because you have the `href` of the link equal to `#`. This is the syntax for a named anchor, and if no name is given after the `#`, the browser just goes to the top of the page. Change your link to `href="javascript:void(0)"` and it won't do that.
zombat
A: 

It works if you change next() to find() or children():

$(function () {
  $('div.box').hide();
  $('a.toggle').click(function () {
    $(this).parents('.entry').find('div.box').toggle(400);
  });
});

This is because next() gets the siblings of the object, while find() searches the descendants. When your code calls next() the object is '.entry' which has no siblings, you can use children() as zombat suggests or find() as I have done to locate div.box as it is a descendant of '.entry'.

beggs
Thanks Beggs. Can I ask.. What exactly is it doing? I mean, the parent tag.. What is its function and when it finds the elements, how do I access them?
Jack
What id does: `$('a.toggle').click(...`: this means add an "onClick()" method to all elements of type 'a' with class 'toggle' `function() { $(this).parents('.entry').find('div.box').toggle(400); }`: anonymous function assigned to the click event saying "take the element that was clicked and find all the ancestors (`parents()`) take any ancestors with class `.entry` and find any of it's descendants with type 'div' and class 'box' and call `toggle()` on that element.If you used `next()` the function would be on a *sibling* of `.entry1 an element at the same level of the DOM tree.
beggs
A: 

Hi Parkim, are you referring to the issue I am having with the page jumping up to the top? I tried this but it still does the same thing. – Jack

nope, because of override, you can't use ".box" selector. you page jumps to the top, because of "#" anchor, you should use "return false" to prevent default "click" handler.

$( "a" ).click( function() { /* your code */ return false; } );
Parkim
A: 

Just as an alternative, I'd use closest and find instead of parent and children respectively, I think it's more a matter of preference though.

$(function () {
  $('div.box').hide();
  $('a.toggle').click(function () {
    $(this).closest('.entry').find('div.box').toggle(400);
    return false;
  });
});
fudgey