Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Accordion: Disable expand on header click, assign to link

Is there a way to disable a sections expansion from the header click, and instead assign the functionality to a link included in the header? The idea is to have a button on the left of the header expand and collapse the header. My hope is that this would allow me to include other elements in the header that would be clickable without expanding/collapsing. Thanks!

like image 440
meburbo Avatar asked Oct 30 '11 20:10

meburbo


3 Answers

Here is a way to do it. Basicly, I'm initializing accordion with disable option and binding click events which first enables accordion, then do the accordion thing and lastly disables accordion.

HTML:

<div class="demo">
  <div id="accordion">
    <h3>
        <a href="#">Section 1</a>
        <a href="#" data-index="0" class="trigger">Expand this</a>
    </h3>
    <div>
      <p>
        Mauris mauris ante, blandit et, ultrices a, suscipit eget, quam. Integer
        ut neque. Vivamus nisi metus, molestie vel, gravida in, condimentum sit
        amet, nunc. Nam a nibh. Donec suscipit eros. Nam mi. Proin viverra leo
        ut odio. Curabitur malesuada. Vestibulum a velit eu ante scelerisque vulputate.
      </p>
    </div>
    <h3>
        <a href="#">Section 2</a>
        <a href="#" data-index="1" class="trigger">Expand this</a>
    </h3>
    <div>
      <p>
        Sed non urna. Donec et ante. Phasellus eu ligula. Vestibulum sit amet
        purus. Vivamus hendrerit, dolor at aliquet laoreet, mauris turpis porttitor
        velit, faucibus interdum tellus libero ac justo. Vivamus non quam. In suscipit
        faucibus urna.
      </p>
    </div>
    <h3>
        <a href="#">Section 3</a>
        <a href="#" data-index="2" class="trigger">Expand this</a>
    </h3>
    <div>
      <p>
        Nam enim risus, molestie et, porta ac, aliquam ac, risus. Quisque lobortis.
        Phasellus pellentesque purus in massa. Aenean in pede. Phasellus ac libero
        ac tellus pellentesque semper. Sed ac felis. Sed commodo, magna quis lacinia
        ornare, quam ante aliquam nisi, eu iaculis leo purus venenatis dui.
      </p>
      <ul>
        <li>
          List item one
        </li>
        <li>
          List item two
        </li>
        <li>
          List item three
        </li>
      </ul>
    </div>
    <h3>
        <a href="#">Section 4</a>
        <a href="#" data-index="3" class="trigger">Expand this</a>
    </h3>
    <div>
      <p>
        Cras dictum. Pellentesque habitant morbi tristique senectus et netus et
        malesuada fames ac turpis egestas. Vestibulum ante ipsum primis in faucibus
        orci luctus et ultrices posuere cubilia Curae; Aenean lacinia mauris vel
        est.
      </p>
      <p>
        Suspendisse eu nisl. Nullam ut libero. Integer dignissim consequat lectus.
        Class aptent taciti sociosqu ad litora torquent per conubia nostra, per
        inceptos himenaeos.
      </p>
    </div>
  </div>
</div>

Javascript:

$(function() {
    $("#accordion").accordion({ disabled: true });
    $(".trigger").click(function() {
        $("#accordion").accordion("enable").accordion("activate", parseInt($(this).data("index"), 10)).accordion("disable");
    });
});
like image 59
Emre Erkan Avatar answered Oct 20 '22 16:10

Emre Erkan


You can simply specify your header element:

$( ".selector" ).accordion({ header: 'h3' });
like image 3
topek Avatar answered Oct 20 '22 16:10

topek


A better way is to unbind the click handler:

    $(function () {
      $("#accordionId").accordion({
         collapsible: true,
         active: false,
         animate: 0,
         icons: false
      });

      $(".ui-accordion-header").unbind('click').click(function() {
         alert('dsds');
      });
   });
like image 2
Franki1986 Avatar answered Oct 20 '22 16:10

Franki1986