views:

69

answers:

2
+4  A: 

Try this

$('li.parent_folder').click(function() {

  // +this+ var refers to the +li+ that was clicked.

  // .children() gets the children of each element in the set of matched elements, optionally filtered by a selector.
  $(this).children('ul.submenu').hide();
});

Here's more information on jQuery .children() method


Edit:

updated method from .find() to .children() based on @Gumbo's recommendation

macek
Better use `children` instead of `find`.
Gumbo
@Gumbo, I suppose you're right. `.children()` is more specific in that it will look at the element's immediate descendants. Thanks :)
macek
thanks for the quick answer. I used your technique but I changed it a bit. I need the "ul.submenu" to hide when I clicked the "li.parent_folder a" and not just the "li.parent folder". This was the final working code:$('li.parent_folder a').click(function() { $(this).parent('li.parent_folder').children('ul.submenu').hide();});thanks a lot!
codedude
oh...mind if I ask you one thing? What would I do if I wanted "ul.submenu" to show again when I clicked it the second time?
codedude
@codedude, jQuery provides a shortcut `$(this).children('ul.submenu').toggle();`
macek
@codedue, you could also try `$(this).children('ul.submenu').slideToggle()` :)
macek
+1  A: 

$ does not maintain any kind of magical context - you'll have to tell it if you want to start looking from a certain element. In an even handler, this points to the element the handler is attached to. Try

$(this).find(">ul.submenu").hide();
Matti Virkkunen