javascript - jQuery Toggle - should close only on clicking the header -
the fiddle: http://jsfiddle.net/bscn3/
scenario: want use nested toggles inside tabbed containers, in fiddle.
my issue: when click on main toggle ("toggle 1") or ("toggle 2"), inner contents display. closes if click on inside. eg. if click on toggle 2, , if click on tab 1 -> nested toggle 1, toggle 2 closes.
i want remain open.
my guess: jquery working closes toggle if related toggle (even text content) clicked.
my need: want toggle close if rectangular headers clicked.
also, if can clean code in such way, don't need write separate js make inner nested toggles work independently of parent or children toggles, great.
currently have 2 toggle js functions written 2 toggles in example.
//toggle
$('.toggle-view li').click(function () { var text = $(this).children('.t'); if (text.is(':hidden')) { text.slidedown('fast'); $(this).children('.toggle').addclass('tactive'); } else { text.slideup('fast'); $(this).children('.toggle').removeclass('tactive'); } });
//toggle l2
$('.toggle-view2 li').click(function () { var text2 = $(this).children('.t2'); if (text2.is(':hidden')) { text2.slidedown('fast'); $(this).children('.toggle2').addclass('tactive2'); } else { text2.slideup('fast'); $(this).children('.toggle2').removeclass('tactive2'); } });
p.s. haven't written js code, using someone's template.
thanks! :)
seems pretty simple solution...
it's happening because toggle activates whenever click anythin inside of li
element (which includes other toggle elements: .toggle2
).
the solution therefore make activate toggle when .toggle
/h6
element clicked , change $(this).children(...)
$(this).siblings(...)
you can use following things (same changes in toggle
, toggle l2
):
//toggle $('.toggle-view li .toggle').click(function () { // changed selector var text = $(this).siblings('.t'); // changed .siblings(...) if (text.is(':hidden')) { text.slidedown('fast'); $(this).addclass('tactive'); // removed .children(...) } else { text.slideup('fast'); $(this).removeclass('tactive'); // removed .children(...) } }); //toggle l2 $('.toggle-view2 li .toggle2').click(function () { var text2 = $(this).siblings('.t2'); if (text2.is(':hidden')) { text2.slidedown('fast'); $(this).addclass('tactive2'); } else { text2.slideup('fast'); $(this).removeclass('tactive2'); } });
or
just use first section...
//toggle $('.toggle-view li .toggle').click(function () { var text = $(this).siblings('.t'); if (text.is(':hidden')) { text.slidedown('fast'); $(this).addclass('tactive'); } else { text.slideup('fast'); $(this).removeclass('tactive'); } });
and rename of .t2
, .toggle2
etc. in html
same first 1 (i.e. .t2
becomes .t
)
Comments
Post a Comment