jQuery UI Accordion events have 3 custom events, these are the methods of activating, beforeActivate and make. The event activate is triggered anytime a header is activated, the event is discharged once the animation is finished acting. The event beforeActivate event is discharged once an accordion header is chosen before the animation begins. The event produced is discharged once the accordion widget is initialized. The table below demonstrates the Accordion Events as shown.
Events |
Description |
activate |
Which is fired when an accordion header is activated and done with the animation performing. |
beforeActivate |
Which is fired when an accordion header is activated and animation yet to be begin. |
create |
Which is triggered before the active content is change. |
Change Event
jQuery UI Accordian activates event come into action if the accordion header is activated and done with the related animation. the code below demonstrates the activate method to control accordion as shown.
[html]
<!DOCTYPE html>
<html>
<head>
<title>jQuery UI Accordion : Using the Change Event</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.3/themes/hot-sneaks/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-2.1.3.js"></script>
<script src="http://code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
<script>
$(document).ready(function() {
var currentStatus;
$("#demoAccordion").accordion({
activate: function(e, ui) {
$(".notify").remove();
currentStatus = $("<div />", {
"class": "notify",
text: [
ui.newHeader.find("a").text(), "header was activated,",
ui.oldHeader.find("a").text(), "header was closed"
].join(" ")
});
currentStatus.insertAfter("#demoAccordion").fadeOut(3000, function(){
$(this).remove();
});
}
});
});
</script>
</head>
<body>
<div id="demoAccordion">
<h2><a href="#">jQuery</a></h2>
<img src="jQuery.png"/>
<h2><a href="#">jquery_mobile</a></h2>
<img src="jquery_mobile.png"/>
<h2><a href="#">jquery_UI</a></h2>
<img src="jquery_UI.png"/>
</div>
</body>
</html>
[/html]
Result
By running the above code in a preferred browser user can get the following output as shown in below image.
beforeActivate
The jQuery UI beforeActivate event is used to activate the specified accordion header and which is also fired if the animated transition begins to perform is as shown in below code.
[html]
<!DOCTYPE html>
<html>
<head>
<title>jQuery UI Accordion : Using the beforeActivate Event</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.3/themes/hot-sneaks/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-2.1.3.js"></script>
<script src="http://code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
<script>
$(document).ready(function() {
var currentStatus;
$("#demoAccordion").accordion({
beforeActivate: function(e, ui) {
currentStatus = $("<div />", {
"class": "notify",
text: [ui.newHeader.find("a").text(),
"-- header was activated, ", ui.oldHeader.find("a" ). text(),
"-- header was closed"].join(" ")
});
currentStatus.insertAfter("#demoAccordion")
.fadeOut(2000, function() {
$(this).remove();
});
}
});
});
</script>
</head>
<body>
<div id="demoAccordion">
<h2><a href="#">jQuery</a></h2>
<img src="jQuery.png"/>
<h2><a href="#">jquery_mobile</a></h2>
<img src="jquery_mobile.png"/>
<h2><a href="#">jquery_UI</a></h2>
<img src="jquery_UI.png"/>
</div>
</body>
</html>
[/html]
Result
By running the above code in a preferred browser user can get the following output as shown in below image.