Add items to a jQuery Accordion with jquery
Asked Answered
G

5

46

I am using the jQuery accordion plugin to make an accordion of some data. Then I want the user to be able to add more data to the accordian. I have setup the accordion to function properly, then I've made a function that prepends a "list item" to the accordion that matches the accordion semantics.

Is it possible to "update" the accordion so that it works with the newly added element, without saving the new data to database and refreshing the page?

Something like this:

.accordion('refresh')

Or something like the live event added in jQuery 1.3, anybody got a clue?

Glynas answered 26/5, 2009 at 13:21 Comment(1)
jquery 1.10 has this functionality working properly. In case you're still stuck on this problem 3 years laterPravit
E
100

I haven't tested it, but this should probably work: Say that you have your accordion with id #accordion

$('#accordion').append('<h3><a href="#">New Paragraph</a></h3><div><p>New data</p></div>')
    .accordion('destroy').accordion();

Basically, just destroy and re-create the accordion.

UPDATE:

Since this answer was written a refresh() method has been added to the accordion widget. It can be invoked by calling:

$( ".selector" ).accordion( "refresh" );

You can read more about this method here

Etruria answered 26/5, 2009 at 13:33 Comment(9)
Googled this for about ten minutes before thinking of looking here.Tony
Thank you - that did the trick for me. One question though! I am adding a section dynamically but when I re-create the accordion it opens the first section again. Is there any way I can stop it from doing that, and kept the previously expanded section? Thanks again.Chlorine
Yep same issue as DanyW, this code will open the first sectionSystematic
Now I haven't worked with this for a year, but one way you can do it is to name the sections (I think it should work if you instead of <a href="#">Paragraph 1</a> write <a href="#paragraph1">Paragraph 1</a> in the title and also add {navigation:true} to your optionsEtruria
this works well for me. is it possible to add some kind of animation / effect to further emphasize to the user that the accordion has been updated?Dirkdirks
$("#accordion").accordion({active:"h3:last"})Didst
FYI, if you want to reuse your code for the initial constructor as well as the refresh, it's a little messy, but you can do this: $('#accordion').accordion().accordion('destroy').accordion();Anana
Regarding @DanyW... Before detroying the accordian, used the active() getter to remember which element is opened (active). Then when recreating the accordian, specify that the remembered section is the active one. Of course this depends upon where the dynamically added elements were appended and the value of the "remembered" active element (an index) may need to be adjusted. Not sure which version of jQuery this was added.Lye
This worked for me too, the only problem was that if you have about 60 items, it would be horrificly slow on mobile devices.Balbriggan
D
8

To add a new section, and keep the old one active:

var active = $('#accordion').accordion('option', 'active');
$('#accordion').append('<h3><a href="#">New Paragraph</a></h3><div><p>New data</p></div>')
    .accordion('destroy').accordion({ active: active});
Dwan answered 24/5, 2012 at 11:29 Comment(0)
J
6

As mentioned above by Shahzad, jQuery UI 1.10 has made this easier; see the documentation.

Japha answered 19/2, 2013 at 17:38 Comment(0)
L
0

You might want to look at the LiveQuery plugin: http://plugins.jquery.com/project/livequery

It allows you to add events and binding after the DOM is loaded.

Layoff answered 26/5, 2009 at 13:33 Comment(0)
E
0

Since i needed a working add/edit/delete vor accordion items i hacked this little javascript functions for this purpose

http://jsfiddle.net/Sd6fC/

h3 and div need an unique id which goes by the following convention

<h3 id="divname_hitm_<uniquenumber>"><h3>

the corresponding div need the following

<div id="divname_ditm_<samenumber as h3"></div>

sample accordion code block

<div id="divname">
    <h3 id="divname_hitm_1><a href="#">Section 1</h3>
    <div id="divname_ditm_1>
        <p>Section 1 Payload</p>
    </div>
    <h3 id="divname_hitm_2><a href="#">Section 2</h3>
    <div id="divname_ditm_2>
        <p>Section 2 Payload</p>
    </div>
</div>

have fun maybe it helps some ppl out there!

Extrauterine answered 13/8, 2012 at 14:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.