javascript - How to set class active to div? -
i have menu in this fiddle , want set active div
on click:
<div class="account-item"> <div class="account-heading active"> <h4 class="account-title"> <a href="#/transactions">2. transaction_history</a> </h4> </div> </div>
i have script selected divs
function setactivediv() { var element = $("a").parent().parent(); if (element.hasclass("active")) { element.removeclass("active"); } else { element.addclass("active"); } }
you need pass clicked element reference
<a href="#/tickets" onclick="setactivediv(this);">1.my_tickets</a>
then
function setactivediv(el) { var element = $(el).closest('.account-heading'); element.toggleclass("active"); }
demo: fiddle
note: since using jquery, try use jquery event handlers instead of inline handlers
so
<a href="#/tickets">1.my_tickets</a>
then
jquery(function ($) { $('.account-group .account-title a').click(function () { var $heading = $(this).closest('.account-heading').toggleclass("active"); $('.account-group .account-heading.active').not($heading).removeclass('active'); }) })
demo: fiddle
Comments
Post a Comment