Handle accordion panel open/close event in JavaScript
Description
The following code shows how to handle accordion panel open/close event.
Example
<!--from w w w . ja v a 2 s.c o m-->
<!doctype html>
<html lang="en">
<head>
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
<link type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/smoothness/jquery-ui.css" rel="stylesheet" />
<script type="text/javascript">
$(function() {
var accOpts = {
//add change event callback
change: function(e, ui) {
alert($(ui.newContent).attr("id") + " was opened, " + $(ui.oldContent).attr("id") + " was closed");
}
};
$("#myAccordion").accordion(accOpts);
});
</script>
</head>
<body>
<div id="myAccordion">
<h3><a class="heading" href="#">Section 1</a></h3>
<div>
<p>P1</p>
</div>
<h3><a class="heading" href="#">Section 2</a></h3>
<div>
<p>P2</p>
</div>
<h3><a class="heading" id="header3" href="#">Section 3</a></h3>
<div>
<p>P3</p>
<ul>
<li>List item one</li>
<li>List item two</li>
<li>List item three</li>
</ul>
</div>
<h3><a class="heading" href="#">Section 4</a></h3>
<div>
<p>P4</p>
</div>
</body>
</html>
]]>
</code>
The code above generates the following result.
Javascript Tutorial Accordion
Build jQuery UI Accordion - Collapse conten...
Build jQuery UI Accordion - Customize icons...
Build jQuery UI Accordion - Default functio...
Build jQuery UI Accordion - Fill space in J...
Build jQuery UI Accordion - No auto height ...
Build jQuery UI Accordion - Open on hoverin...
Build jQuery UI Accordion - Sortable in Jav...
Create option array and pass to create acco...
Customize accordion title bar and content i...
Enable or disable accordion panel in JavaSc...
Set active panel for accordion in JavaScrip...
Turn the accordion panel to plain html in J...
Build jQuery UI Accordion - Collapse conten...
Build jQuery UI Accordion - Customize icons...
Build jQuery UI Accordion - Default functio...
Build jQuery UI Accordion - Fill space in J...
Build jQuery UI Accordion - No auto height ...
Build jQuery UI Accordion - Open on hoverin...
Build jQuery UI Accordion - Sortable in Jav...
Create option array and pass to create acco...
Customize accordion title bar and content i...
Enable or disable accordion panel in JavaSc...
Handle accordion panel open/close event in ...
Set active accordion panel in JavaScriptSet active panel for accordion in JavaScrip...
Turn the accordion panel to plain html in J...