Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Javascript

jQuery UI Accordion - Cancel Change

0.00/5 (No votes)
4 Oct 2012CPOL 15.5K  
Simple way to prevent users from changing the active panel on the jQuery UI Accordion control

Introduction

jQuery UI Accordion is a great control out-of-the-box but as usual it's not enough, we always need more!
So here's the thing: I needed a simple way, without much hacking, to let me handle if the user could expand/collapse a panel or not.
If you look around, there are some ideas but I couldn't find one that actually consistently worked so here's my way of doing it.

Using the Code

The idea is to handle the header click and this way decide to activate the panel or not. In the sample code, just use the checkbox to lock or unlock the panels activation.

Here's the jsfille link and the same code below.

HTML

HTML
<input id="chkLock" type="checkbox"/>
<label for="chkLock">Lock Accordion</label>

<div id="myaccordion">
    <h3>Header 1</h3>
    <div>Content 1</div>
    <h3>Header 2</h3>
    <div>Content 2</div>
    <h3>Header 3</h3>
    <div>Content 3</div>
</div>

JavaScript

JavaScript
$('#myaccordion').accordion();

$("#myaccordion h3")
    .off('click')
    .on('click', function(){
        if ($('#chkLock').prop('checked') == false) {
            var index = $('#myaccordion h3').index(this);
            $('#myaccordion').accordion("activate", index);
        }
    });

Points of Interest

There are always multiple ways of doing the same thing, the difficulty is finding the simplest one. Smile | :)

History

First version!
Hope to hear from you guys about even simpler solutions!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)