SANDWIDI SANDWIDI - 1 month ago 8
HTML Question

Open all sections in jQuery UI's accordion after page loading

Hello i would like to have jQuery Ui's accordion opened after page loaded. For that i have found the below code on this site.

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="../image/logo.png" href="image/logo.png" />
<title>Survey</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script type="text/javascript">
$( function() {
$('#accordion').accordion({
collapsible:true,
heightStyle: 'content',
beforeActivate: function(event, ui) {
// The accordion believes a panel is being opened
if (ui.newHeader[0]) {
var currHeader = ui.newHeader;
var currContent = currHeader.next('.ui-accordion-content');
// The accordion believes a panel is being closed
} else {
var currHeader = ui.oldHeader;
var currContent = currHeader.next('.ui-accordion-content');
}
// Since we've changed the default behavior, this detects the actual status
var isPanelSelected = currHeader.attr('aria-selected') == 'true';

// Toggle the panel's header
currHeader.toggleClass('ui-corner-all',isPanelSelected).toggleClass('accordion-header-active ui-state-active ui-corner-top',!isPanelSelected).attr('aria-selected',((!isPanelSelected).toString()));

// Toggle the panel's icon
currHeader.children('.ui-icon').toggleClass('ui-icon-triangle-1-e',isPanelSelected).toggleClass('ui-icon-triangle-1-s',!isPanelSelected);

// Toggle the panel's content
currContent.toggleClass('accordion-content-active',!isPanelSelected)
if (isPanelSelected) { currContent.slideUp(); } else { currContent.slideDown(); }

return false; // Cancel the default action
}
});
$("#accordion> div").accordion({
header: "h3",
autoHeight: false,
collapsible: true,
icons: icons
});
});
</script>
</head>
<body>
<div id="accordion">
<h3>First header</h3>
<div>First content panel</div>
<h3>Second header</h3>
<div>Second content panel</div>
</div>
</body>




My difficult concerns how i can proceed to make all accordion opened after page loaded even if all things are right.
Thank you!

Answer

Just show the accordion content, like this:

$( function() {
  $('#accordion').accordion({
    collapsible:true,
    heightStyle: 'content',
    create: function( event, ui ) {
      $('#accordion .ui-accordion-content').show();
    }
  });
});
<!doctype html>
<html lang="en">
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="icon" type="../image/logo.png" href="image/logo.png" />
      <title>Survey</title>
      <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
      <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
      <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
      <script type="text/javascript">
          
      </script>
    </head>
    <body>
        <div id="accordion">
          <h3>First header</h3>
          <div>First content panel</div>
          <h3>Second header</h3>
          <div>Second content panel</div>
        </div>
    </body>