jQuery Mobile loading accordion dynamically not functioning

jQuery Mobile loading accordion dynamically not functioning

I am loading the jQuery Mobile accordion dynamically via JavaScript and having it create the elements and inject the HTML into the page. Now when I do this, I don't get the styles, I don't get the theme and I don't get the functionally. It basically does not work. Does anyone know why it is doing this? let me know if you need anymore info.

  1. $('#film').attr({'data-role': 'collapsible-set', 'data-theme': 'c', 'data-content-theme': 'd'});

  2. var sort_by = function(field, reverse, primer){
  3. var key = function(x) {return primer ? primer(x[field]) : x[field]};
  4. return function(a,b) {
  5. var A = key(a), B = key(b);
  6. return ((A < B) ? -1 : (A > B) ? +1 : 0) * [-1,1][+!!reverse];
  7. }
  8. }

  9. $.ajax({
  10. type: 'GET',
  11. url: 'list',
  12. async: false,
  13. jsonpCallback: 'fl',
  14. contentType: 'application/json',
  15. dataType: 'jsonp',
  16. success: function(data) {
  17. var tmp = [];
  18. for(var i=0;i<data.nodes.length;i++) {
  19. tmp.push(data.nodes[i].node);
  20. }
  21. tmp.sort(sort_by('title', true));
  22. var div = document.createElement('div');
  23. $('#film').append(div);
  24. $(div).attr( 'data-role', 'collapsible' );
  25. var heading = tmp[0]['title'][0];
  26. var h3 = document.createElement('h3');
  27. h3.innerHTML = heading;
  28. $(div).append(h3);
  29. h3=null;
  30. var title = '';
  31. for(var i=0;i<tmp.length;i++) {
  32. if(tmp[i]['title'] != title){ title=tmp[i]['title'];
  33. if(tmp[i]['title'][0] !== heading){
  34. heading = tmp[i]['title'][0];
  35. var div = document.createElement('div');
  36. $('#film').append(div);
  37. $(div).attr( 'data-role', 'collapsible' );
  38. var h3 = document.createElement('h3');
  39. h3.innerHTML = heading; 
  40. $(div).append(h3);
  41. h3=null;
  42. }
  43. var p = document.createElement('p');
  44. p.innerHTML += "<a>"+tmp[i]['title']+"</a>";
  45. $(div).append(p);
  46. p=null;
  47. }
  48. }
  49. }
  50. });