Search code examples
jquerytoggleclass

jQuery target only elements within parent div


I have a number of widgets on a page and I want to re-use classes throughout. I have a question mark icon that should toggle visibility between 2 divs inside the widget.

The problem is I can't get my code to only target the current widget. I have tried the usual techniques but none seem to be working here.

The fiddle is here, http://jsfiddle.net/clintongreen/9hvVn/

The click function in question works from the question mark icon. The divs that need toggleClass are .widget-content and .widget-content_next

Thanks :)


Solution

  • widget-content and widget-content_next aren't direct ancestors of the link, I think you want something like this:

     $(this).closest(".widget").find('.widget-content').toggleClass("hidden");
     $(this).closest(".widget").find(".widget-content_next").toggleClass("hidden");