title: Dynamically adding collapsible elements
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/4214538/dynamically-adding-collapsible-elements">Dynamically adding collapsible elements</a>  </p>
  </blockquote>
  
  
  
  <p>I'm trying to create dynamic content for a mobile website but I am having trouble with collapsible elements displaying properly. I looked at some similar questions posted here and I was unable to successfully implement the the following fix:</p>
  
  <pre><code>$('div[data-role=collapsible]').collapsible();
  </code></pre>
  
  <p>The following is my javascript:</p>
  
  <pre><code>function loadContent() {
  
      $(document).ready(function () {
          $.ajax({
              type: "GET",
              url: "company.xml",
              dataType: "xml",
              success: parseCompany
          });
      });
  
      $(document).ready(function () {
          $.ajax({
              type: "GET",
              url: "italy.xml",
              dataType: "xml",
              success: parseTour
          });
      });
  }
  
  function parseCompany(xml) {
      $(xml).find("company").each(function () {
          $("#company").html("&lt;h1&gt;" + $(this).find("name").text() + "&lt;/h1&gt;");
  
          var link = $(this).find("contact-page").text();
          var message = "&lt;p&gt;Telephone: " + $(this).find("booking-phone").text() + "&lt;/p&gt;";
          message += "&lt;p&gt;&lt;a href=" + link + "&gt;Company Website&lt;/a&gt;&lt;/p&gt;";
  
          $("#company-info").html(message);
      });
  }
  
  function parseTour(xml) {
      $(xml).find("package").each(function () {
          //var message = "&lt;div data-role='collapsible' data-collapsed='true' data-theme='c'&gt;";
          $("#package-name").html("  ").append($(this).attr("title"));
          var message = "&lt;p&gt;Basecost: $" + $(this).attr("baseCost") + "&lt;/p&gt;";
          message += "&lt;p&gt;Length: " + $(this).attr("length") + " days &lt;/p&gt;&lt;/div&gt;";
          $("#package").html(message);
      });
  }
  </code></pre>
  
  <p>The following is the html:</p>
  
  <pre><code>    &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  
  &lt;head&gt;
      &lt;title&gt;Tour Booking Made Easy&lt;/title&gt;
      &lt;script src="tourinfo.js" type="text/javascript"&gt;&lt;/script&gt;
      &lt;link href="http://code.jquery.com/mobile/latest/jquery.mobile.min.css" rel="stylesheet" type="text/css" /&gt;
      &lt;script src="jquery-1.6.2.js" type="text/javascript"&gt;&lt;/script&gt;
      &lt;script src="http://code.jquery.com/mobile/latest/jquery.mobile.min.js"&gt;&lt;/script&gt;
  
  
  &lt;/head&gt;
  &lt;body onload="loadContent()"&gt;
      &lt;div data-role="page"&gt;
   &lt;div id="company" data-role="header" data-theme="b"&gt;&lt;/div&gt;
  
   &lt;div id="company-info" data-role="content" data-theme="b"&gt;&lt;/div&gt;
  
   &lt;div data-role="collapsible" data-theme="c"&gt;
              &lt;h2 id="package-name"&gt;test&lt;/h2&gt;
              &lt;div id="package"&gt;
              &lt;div id="stops"&gt;&lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>The collapsible feature does function but doesn't appear like it should. I have also tried inserting the collapsible div tags from the javascript into some empty div tags in the html but then the content doesn't appear at all. Any help will be greatly appreciated. Thank you.</p>
  
  <p>-Manav</p>