Hi there,

Here is the simple answer: make use of the "this" keyword.

Here is an example:

$('li.hide-comm-single').click(function() {
  $(this).parents('div.comment').find('div.content').hide();
  return false;
});

I'm assuming that, as your markup shows, the only other child of div.comment will be div.content.

You don't really need the <a> tags in there if you're attaching the click handler to the <li> anyway.

If you're a fan of XPath, you could do it this way, too:

$('li.hide-comm-single').click(function() {
        $('../../div.content', this).hide();
  return false;
});


Hope that helps.


--Karl
_________________
Karl Swedberg
www.englishrules.com
www.learningjquery.com



On Jun 14, 2007, at 8:36 PM, bsuttis wrote:


Sorry for the vague subject, I rewrote it a few times and it still
doesn't make much sense, hopefully what I say below does.

I'm trying to implement jQuery into my comments to enable users to
show/hide them as they read them.

My problem occurs that using my current jQuery code, every comment div
gets closed when clicking on one, while I would like it to only close
the div the user clicks on. The comment itself is wrapped in a <div
class="comment"></div> while the Show/Hide links are within a <ul> in
the <div> (explaining the not('ul') part of my code).

My comment div looks something like this:

<div class="comment">
  <ul><li class="show-comm-single"><a href="#" title="Show
Comment">Show</a></li><li class="hide-comm-single"><a href="#"
title="Hide Comment">Hide</a></li></ul>
  <div class="content"><p>comment here</p></div>
</div>

And my jQuery code:

        $('li.hide-comm-single').click(function(){
                $('.comment').children().not('ul').hide();
                return false;
        });

With the current code, each comment on my page is hidden (so for
example, if the page has 3 comments, and I only mean to click the
first comment, but clicking it closes the other 2 comments as well).

My question, is it possible to write the code so that 1 <div
class="comment"> at a time is closed instead of every one? Is there
something I'm missing that allows just a unique .comment to be closed
without having to use something silly like css ids.


Reply via email to