Add items to a jQuery Accordion with jquery

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 21:01:55
Jimmy Stenke

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

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});
Mark D.

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

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.

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!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!