javascript - changing plus sign to minus when collapsed while keeping link -
i'm pretty new jquery, admit don't know i'm doing. top menu item text , not it's own link, has dropdown. i'm trying add plus , minus sign before text toggles while keeping original content clickable.
html
<div class="menu-clinical-materials"> <ul> <li class="menu-item-has-children"><a>clinical studies & articles</a> <ul class="sub-menu" style="display: none;"> <li><a target="_blank" href="">link 1</a></li> <li><a target="_blank" href="">link 2</a></li> <li><a target="_blank" href="">link 3</a></li> <li><a target="_blank" href="">link 4</a></li> <li><a target="_blank" href="">link 5</a></li> </ul> </li> <li class="menu-item-has-children"><a>counseling sheets</a> <ul class="sub-menu" style="display: none;"> <li><a target="_blank" href="">link 1</a></li> <li><a target="_blank" href="">link 2</a></li> <li><a target="_blank" href="">link 3</a></li> <li><a target="_blank" href="">link 4</a></li> <li><a target="_blank" href="">link 5</a></li> </ul> </li> </ul>
js
jquery.noconflict(); jquery(document).ready(function () { jquery(".menu-item-has-children:has(ul)").prepend("<span class=\"expander\">+</span>"); jquery(".expander").click(function () { jquery(this).html(jquery(this).html() == "+" ? "-" : "+"); jquery(this).toggleclass("expanded").siblings("ul").slidetoggle(); return false; }).eq(0).addclass("expanded").end().slice(1).siblings("ul").hide(); });
so want "clinical studies & articles" clickable, not +/- sign.
edit
i able work way wanted using different method. i'd still know how .click function instead of toggle though. i'd appreciate it. here's fiddle workaround fiddle
here changed js
//<![cdata[ jquery.noconflict(); jquery(document).ready(function () { jquery(".menu-item-has-children:has(ul)").prepend("<span class=\"plus-minus\">+</span>"); jquery(".sub-menu").addclass('expander').hide(); jquery('.menu-item-has-children').parent().toggle(function () { jquery(".plus-minus").text("-"); jquery(".expander").slidedown(); }, function () { jquery(".plus-minus").text("+"); jquery(".expander").slideup(); }) });
improved demo. use event.stoppropagation() prevent click on inner links bubble , close whole block.
//<![cdata[ jquery.noconflict(); jquery(document).ready(function () { jquery(".menu-item-has-children:has(ul)").prepend("<span class=\"expander\">+</span>"); jquery(".menu-item-has-children").click(function () { buttontext = jquery(this).children('.expander').html() == "+" ? "-" : "+"; jquery(this).children('.expander').html(buttontext); jquery(this).children('.expander') .toggleclass("expanded").siblings("ul").slidetoggle(); return false; }).eq(0).slice(1).siblings("ul").hide(); }); //prevent close on click of inner links. jquery(".sub-menu > li").click(function (e) { e.stoppropagation(); }); //]]>
Comments
Post a Comment