<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jsrender/1.0.5/jsrender.min.js"></script>
</head>
<body>
<div id="main-quiz">
</div>
<button class="btn-submit">Submit answers</button>
<script id="tmplForm" type="text/x-jsrender">
<section id="section-{{:id}}" data-id="{{:id}}">
<div class="section-top">
<hr class="hideOnMobile">
<h1 class="top-title">#{{:id}} {{:category}}</h1>
<p class="title optionsTitle {{:category}} titleFor_{{:id}}">{{:header}}</p>
</div>
<div class="section-bottom">
And this is bottom div
{{for questions}}
<div data-id="{{:id}}" class="question question{{:category_id}}" data-parentid="{{:#parent.data.id}}">
<p class="questionP questionId_{{:id}}">{{:question}}</p>
<ul id="questionId-{{:id}}" class="answerContainer">
{{for answers}}
<li data-id="{{:id}}" data-parentid="{{:#parent.parent.data.id}}">
<label for="answer_{{:#parent.parent.parent.parent.data.id}}_{{:#parent.parent.data.id}}_{{:id}}">
<input data-id="{{:#parent.parent.parent.parent.data.id}}_{{:#parent.parent.data.id}}_{{:id}}" type="radio"
name="answer_{{:#parent.parent.parent.parent.data.id}}_{{:#parent.parent.data.id}}"
id="answer_{{:#parent.parent.parent.parent.data.id}}_{{:#parent.parent.data.id}}_{{:id}}"
value="{{:answer}}" />
{{:answer}}
</label>
</li>
{{/for}}
</ul>
</div>
{{/for}}
</div>
</section>
</script>
</body>
</html>
$(function() {
var formData = [];
var settings = [
// categories
'https://api.myjson.com/bins/nilct',
// questions
'https://api.myjson.com/bins/1178p1',
// answers
'https://api.myjson.com/bins/qhft1',
];
// if Promises are available, use this code
/*
Promise.all([
getData(settings[0]),
getData(settings[1]),
getData(settings[2])
]).then(function(allData) {
var categories = allData[0],
questions = allData[1],
answers = allData[2];
formData = categories;
// now that we have all questions, append them to categories
formData.forEach(function(x) {
x.questions = questions;
// now that we have all answers, append them to questions
x.questions.forEach(function(y) {
y.answers = answers;
});
});
processData(formData, '#tmplForm', '#main-quiz');
$('.btn-submit').show();
}); */
// if you can't use Promises, use the code below
$.getJSON(settings[0], function(data) {
formData = data;
$.getJSON(settings[1], function(data) {
// now that we have all questions, append them to categories
formData.forEach(function(x) {
x.questions = data;
});
$.getJSON(settings[2], function(data) {
// now that we have all answers, append them to questions
formData.forEach(function(x) {
x.questions.forEach(function(y) {
y.answers = data;
});
});
// console.log(JSON.stringify(formData, null, 4))
processData(formData, '#tmplForm', '#main-quiz');
$('.btn-submit').show();
});
});
});
/* */
// submit button event
$('.btn-submit').on('click', submitData);
});
function processData(data, scriptTmpl, appendTo) {
// get the template
var template = $.templates(scriptTmpl);
// convert to HTML
var htmlOutput = template.render(data);
// append to html element
$(appendTo).append(htmlOutput);
}
function submitData(evt) {
var answersData = [];
var questions = $('#main-quiz .question');
console.log('FOUND ' + questions.length + ' questions');
questions.each(function (i, q) {
var question = $(q);
var answer = $(q).find('input[type="radio"]:checked');
answersData.push({
id: answer.data('id') || '',
answer: answer.val() || '' });
});
console.log(JSON.stringify(answersData, null, 4));
}
function getData(url) {
return new Promise(function(resolve, reject) {
$.getJSON(url, function(data) { resolve(data); });
});
}
Output
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |