jquery - Collapsing dynamically added tabs in accordion -
here sample.
$(function() { var icons = { header: "ui-icon-circle-arrow-e", headerselected: "ui-icon-circle-arrow-s" }; $( "#accordion" ).accordion({ icons: icons, collapsible: true }); $( "#header1" ).click(function(e) { return false; $( "#accordion" ).accordion( "option", "icons", false ); }); }); $('#accordion .accclicked') .off('click') .click(function(){ $(this).next().toggle('fast'); }); $('#add').click(function() { var $tab="<h3 id='header3' class='accclicked'><a href='#'>section 3</a></h3><div>bonjour stackoverflow</div>"; $('#accordion').append($tab); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <div id="accordion"> <h3 id="header1" class="accclicked"><a href="#">section 1</a></h3> <div> morning stackoverflow</div> <h3 id="header2" class="accclicked"><a href="#">section 2</a></h3> <div>buongiorno stackoverflow</div> <h3 id="header3" class="accclicked"><a href="#">section 3</a></h3> <div>bonjour stackoverflow</div> </div> <button id="add"> add tab </button>
i can add new tab can not collapse it. searched , found should somehow update dom, didn't find how can it.
i use jquery-ui accordion, problem is, automatically closes opened tabs when open tab, don't need that. here link it: jquery-ui accordion
thank you.
you can turn click rewrite function , run again each tab add:
$(function() { var icons = { header: "ui-icon-circle-arrow-e", headerselected: "ui-icon-circle-arrow-s" }; $("#accordion").accordion({ icons: icons, collapsible: true }); $("#header1").click(function(e) { return false; $("#accordion").accordion("option", "icons", false); }); }); // here change to: function resettabclick() { $('#accordion .accclicked') .off('click').click(function(){ $(this).next().toggle('fast'); }); } resettabclick(); $('#add').click(function() { var $tab="<h3 id='header3' class='accclicked'><a href='#'>section 3</a></h3><div>bonjour stackoverflow</div>"; $('#accordion').append($tab); resettabclick(); });
Comments
Post a Comment