How to load content in jQueryUI accordion dynamically


Hey guys,

Do you aware of the fact that jQuery UI allows to load content dynamically for tabs but not for accordion?. So here I came up with a simple solution. You just have to pass a url in href attribute for each accordion header as shown below:

href="ajax1.html">Section 1>/h3>

Loading... Please wait.

<h3><a href="ajax2.html">Section 2</a></h3>

Loading... Please wait.

</div>

Accordion UI provides many callback functions but we are going to use one of them, the most important callback function,  activate which gets called when you click on any accordion panel to open it. We are going to use it to load content using AJAX.

  $(function() {
     $( "#accordion" ).accordion({
        collapsible: true,
        active : false,
        activate: function (e, ui) {
          $url = $(ui.newHeader[0]).children('a').attr('href');
	  $.get($url, function (data) {
	     $(ui.newHeader[0]).next().html(data);
          });
        }
     });
  });

You can also download the code from github. Happy Coding 🙂

If you found this article useful in anyway, feel free to donate me and receive my dilettante painting as a token of appreciation for your donation.

9 thoughts on “How to load content in jQueryUI accordion dynamically

  1. Nice. But I am looking for more. So this works great the first time. The second time a user tries to open the accordion though, I should not need to reload the content, it just stays on to save a lot of traffic and waiting. Do you have a way for that? Actually I think I can just check if some contents exists?

    • Oops, I’ve replaced the hotlinked jquery/jqueryui. And also the API has also changed which does not support `change` event. Its replaced by `activate` now. Thanks for informing me. Cheers!

Leave a reply to Kishore Relangi Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.