Skip to content

Instantly share code, notes, and snippets.

@adatta02
Last active December 10, 2015 12:49
Show Gist options
  • Save adatta02/4437007 to your computer and use it in GitHub Desktop.
Save adatta02/4437007 to your computer and use it in GitHub Desktop.
var questionArray = { };
var questionLabels = [];
var tbl = $("<table class='js-format-table'><thead><tr></tr></thead><tbody></tbody></table>");
var boilerplate;
$('div[id^="question"]').each(function(i) {
if( $(this).hasClass("boilerplate") ){
boilerplate = $(this);
return true;
}
var label = $.trim($(this).find(".questiontext:first").text());
$(this).hide();
if( !questionArray[label] ){
questionArray[label] = [];
questionLabels.push(label);
$("<th>" + label + "</th>").appendTo($(tbl).find("thead tr"));
}
var el = $(this).find(".question").remove();
questionArray[label].push( el );
});
var num = questionArray[questionLabels[0]].length;
for(var j = 0; j < num; j++){
var tr = $("<tr></tr>");
for(var i = 0; i < questionLabels.length; i++){
var html = "<td>" + $(questionArray[questionLabels[i]].pop()).html() + "</td>";
$(html).appendTo(tr);
}
$(tr).appendTo( $(tbl).find("tbody") );
}
$(boilerplate).after(tbl);
$("<style>.js-format-table input[type='text'], .js-format-table select { width: 110px; padding: 2px; }</style>").appendTo("body");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment