HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URL's added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<header class="header bg-primary">
<div class="left-title">JS Quiz</div>
<div class="right-title">Total Questions: <span id="tque"></span></div>
<div class="clearfix"></div>
</header>
<div class="content">
<div class="container-fluid">
<div class="row">
<div class="col-sm-12">
<div id="result" class="quiz-body">
<form name="quizForm" onSubmit="">
<fieldset class="form-group">
<h4><span id="qid">1.</span> <span id="question"></span></h4>
<div class="option-block-container" id="question-options">
</div> <!-- End of option block -->
</fieldset>
<button name="previous" id="previous" class="btn btn-success">Previous</button>
<button name="next" id="next" class="btn btn-success">Next</button>
</form>
</div>
</div> <!-- End of col-sm-12 -->
</div> <!-- End of row -->
</div> <!-- ENd of container fluid -->
</div> <!-- End of content -->
.content { margin-top:54px; }
.header {padding:15px; position:fixed; top:0; width:100%; z-index:9999; }
.left-title { width:80px; color:#FFF; font-size:18px; float:left; }
.right-title { width:150px; text-align:right; float:right; color:#FFF; }
.quiz-body { margin-top:15px; padding-bottom:50px; }
.option-block-container { margin-top:20px; max-width:420px; }
.option-block { padding:10px; background:aliceblue; border:1px solid #84c5fe; margin-bottom:10px; cursor:pointer; }
.result-question { font-weight:bold; }
.c-wrong { margin-left:20px; color:#FF0000; }
.c-correct { margin-left:20px; color:green; }
.last-row { border-bottom:1px solid #ccc; padding-bottom:25px; margin-bottom:25px; }
.res-header { border-bottom:1px solid #ccc; margin-bottom:15px; padding-bottom:15px; }
/* Created and coded by Abhilash Narayan */
/* Quiz source: w3schools.com */
var quiz = { "JS" : [
{
"id" : 1,
"question" : "Inside which HTML element do we put the JavaScript?",
"options" : [
{"a": "<script>",
"b":"<javascript>",
"c":"<scripting>",
"d":"<js>"}
],
"answer":"<script>",
"score":0,
"status": ""
},
{
"id" : 2,
"question" : "Where is the correct place to insert a JavaScript?",
"options" : [
{"a": "The <head> section",
"b":"The <body> section",
"c":"Both the <head> section and the <body> section are correct"}
],
"answer":"Both the <head> section and the <body> section are correct",
"score":0,
"status": ""
},
{
"id" : 3,
"question" : "What is the correct syntax for referring to an external script called 'xxx.js'?",
"options" : [
{"a": "<script href="xxx.js">",
"b":"<script name="xxx.js">",
"c":"<script src="xxx.js">"}
],
"answer":"<script src="xxx.js">",
"score":0,
"status": ""
},
{
"id" : 4,
"question" : "The external JavaScript file must contain the <script> tag.",
"options" : [
{"a": "True",
"b":"False"
}
],
"answer":"False",
"score":0,
"status": ""
},
{
"id" : 5,
"question" : "How do you write "Hello World" in an alert box?",
"options" : [
{"a": "alertBox("Hello World");",
"b":"msg("Hello World");",
"c":"alert("Hello World");",
"d":"msgBox("Hello World");",
}
],
"answer":"alert("Hello World");",
"score":0,
"status": ""
},
{
"id" : 6,
"question" : "How do you create a function in JavaScript?",
"options" : [
{"a": "function myFunction()",
"b":"function:myFunction()",
"c":"function = myFunction()",
}
],
"answer":"function myFunction()",
"score":0,
"status": ""
},
{
"id" : 7,
"question" : "How do you call a function named "myFunction"?",
"options" : [
{"a": "call function myFunction()",
"b":"call myFunction()",
"c":"myFunction()",
}
],
"answer":"myFunction()",
"score":0,
"status": ""
},
{
"id" : 8,
"question" : "How to write an IF statement in JavaScript?",
"options" : [
{"a": "if i = 5 then",
"b":"if i == 5 then",
"c":"if (i == 5)",
"d":" if i = 5",
}
],
"answer":"if (i == 5)",
"score":0,
"status": ""
},
{
"id" : 9,
"question" : "Which of the following is a disadvantage of using JavaScript?",
"options" : [
{"a": "Client-side JavaScript does not allow the reading or writing of files.",
"b":"JavaScript can not be used for Networking applications because there is no such support available.",
"c":"JavaScript doesn't have any multithreading or multiprocess capabilities.",
"d":"All of the above."
}
],
"answer":"All of the above.",
"score":0,
"status": ""
},
{
"id" : 10,
"question" : "How to write an IF statement for executing some code if "i" is NOT equal to 5?",
"options" : [
{"a": "if (i <> 5)",
"b":"if i <> 5",
"c":"if (i != 5)",
"d":"if i =! 5 then",
}
],
"answer":"if (i != 5)",
"score":0,
"status": ""
},
{
"id" : 11,
"question" : "How does a WHILE loop start?",
"options" : [
{"a": "while i = 1 to 10",
"b":"while (i <= 10; i++)",
"c":"while (i <= 10)"
}
],
"answer":"while (i <= 10)",
"score":0,
"status": ""
},
{
"id" : 12,
"question" : "How does a FOR loop start?",
"options" : [
{"a": "for (i = 0; i <= 5)",
"b":"for (i = 0; i <= 5; i++)",
"c":"for i = 1 to 5",
"d":"for (i <= 5; i++)"
}
],
"answer":"for (i = 0; i <= 5; i++)",
"score":0,
"status": ""
},
{
"id" : 13,
"question" : "How can you add a comment in a JavaScript?",
"options" : [
{"a": "//This is a comment",
"b":"‚This is a comment",
"c":"<!--This is a comment-->"
}
],
"answer":"//This is a comment",
"score":0,
"status": ""
},
{
"id" : 14,
"question" : "How to insert a comment that has more than one line?",
"options" : [
{"a": "/*This comment has more than one line*/",
"b":"//This comment has more than one line//",
"c":"<!--This comment has more than one line-->"
}
],
"answer":"/*This comment has more than one line*/",
"score":0,
"status": ""
},
{
"id" : 15,
"question" : "What is the correct way to write a JavaScript array?",
"options" : [
{"a": "var colors = (1:"red", 2:"green", 3:"blue")",
"b":"var colors = ["red", "green", "blue"]",
"c":"var colors = 1 = ("red"), 2 = ("green"), 3 = ("blue")",
"d":"var colors = "red", "green", "blue""
}
],
"answer":"var colors = ["red", "green", "blue"]",
"score":0,
"status": ""
},
{
"id" : 16,
"question" : "How do you round the number 7.25, to the nearest integer?",
"options" : [
{"a": "rnd(7.25)",
"b":"Math.round(7.25)",
"c":"Math.rnd(7.25)",
"d":"round(7.25)"
}
],
"answer":"Math.round(7.25)",
"score":0,
"status": ""
},
{
"id" : 17,
"question" : "How do you find the number with the highest value of x and y?",
"options" : [
{"a": "Math.max(x, y)",
"b":"Math.ceil(x, y)",
"c":"top(x, y)",
"d":"ceil(x, y)"
}
],
"answer":"Math.max(x, y)",
"score":0,
"status": ""
},
{
"id" : 18,
"question" : "What is the correct JavaScript syntax for opening a new window called "w2"?",
"options" : [
{"a": "w2 = window.new("http://www.w3schools.com");",
"b":"w2 = window.open("http://www.w3schools.com");"
}
],
"answer":"w2 = window.open("http://www.w3schools.com");",
"score":0,
"status": ""
},
{
"id" : 19,
"question" : "JavaScript is the same as Java.",
"options" : [
{"a": "true",
"b":"false"
}
],
"answer":"false",
"score":0,
"status": ""
},
{
"id" : 20,
"question" : "How can you detect the client’s browser name?",
"options" : [
{"a": "navigator.appName",
"b":"browser.name",
"c":"client.navName"
}
],
"answer":"navigator.appName",
"score":0,
"status": ""
},
{
"id" : 21,
"question" : "Which event occurs when the user clicks on an HTML element?",
"options" : [
{"a": "onchange",
"b":"onclick",
"c":"onmouseclick",
"d":"onmouseover"
}
],
"answer":"onclick",
"score":0,
"status": ""
},
{
"id" : 22,
"question" : "How do you declare a JavaScript variable?",
"options" : [
{"a": "var carName;",
"b":"variable carName;",
"c":"v carName;"
}
],
"answer":"var carName;",
"score":0,
"status": ""
},
{
"id" : 23,
"question" : "Which operator is used to assign a value to a variable?",
"options" : [
{"a": "*",
"b":"-",
"c":"=",
"d":"x"
}
],
"answer":"=",
"score":0,
"status": ""
},
{
"id" : 24,
"question" : "What will the following code return: Boolean(10 > 9)",
"options" : [
{"a": "NaN",
"b":"false",
"c":"true"
}
],
"answer":"true",
"score":0,
"status": ""
},
{
"id" : 25,
"question" : "Is JavaScript case-sensitive?",
"options" : [
{"a": "No",
"b":"Yes"
}
],
"answer":"Yes",
"score":0,
"status": ""
}
]
}
var quizApp = function() {
this.score = 0;
this.qno = 1;
this.currentque = 0;
var totalque = quiz.JS.length;
this.displayQuiz = function(cque) {
this.currentque = cque;
if(this.currentque < totalque) {
$("#tque").html(totalque);
$("#previous").attr("disabled", false);
$("#next").attr("disabled", false);
$("#qid").html(quiz.JS[this.currentque].id + '.');
$("#question").html(quiz.JS[this.currentque].question);
$("#question-options").html("");
for (var key in quiz.JS[this.currentque].options[0]) {
if (quiz.JS[this.currentque].options[0].hasOwnProperty(key)) {
$("#question-options").append(
"<div class='form-check option-block'>" +
"<label class='form-check-label'>" +
"<input type='radio' class='form-check-input' name='option' id='q"+key+"' value='" + quiz.JS[this.currentque].options[0][key] + "'><span id='optionval'>" +
quiz.JS[this.currentque].options[0][key] +
"</span></label>"
);
}
}
}
if(this.currentque <= 0) {
$("#previous").attr("disabled", true);
}
if(this.currentque >= totalque) {
$('#next').attr('disabled', true);
for(var i = 0; i < totalque; i++) {
this.score = this.score + quiz.JS[i].score;
}
return this.showResult(this.score);
}
}
this.showResult = function(scr) {
$("#result").addClass('result');
$("#result").html("<h1 class='res-header'>Total Score: " + scr + '/' + totalque + "</h1>");
for(var j = 0; j < totalque; j++) {
var res;
if(quiz.JS[j].score == 0) {
res = '<span class="wrong">' + quiz.JS[j].score + '</span><i class="fa fa-remove c-wrong"></i>';
} else {
res = '<span class="correct">' + quiz.JS[j].score + '</span><i class="fa fa-check c-correct"></i>';
}
$("#result").append(
'<div class="result-question"><span>Q ' + quiz.JS[j].id + '</span> ' + quiz.JS[j].question + '</div>' +
'<div><b>Correct answer:</b> ' + quiz.JS[j].answer + '</div>' +
'<div class="last-row"><b>Score:</b> ' + res +
'</div>'
);
}
}
this.checkAnswer = function(option) {
var answer = quiz.JS[this.currentque].answer;
option = option.replace(/\</g,"<") //for <
option = option.replace(/\>/g,">") //for >
option = option.replace(/"/g, """)
if(option == quiz.JS[this.currentque].answer) {
if(quiz.JS[this.currentque].score == "") {
quiz.JS[this.currentque].score = 1;
quiz.JS[this.currentque].status = "correct";
}
} else {
quiz.JS[this.currentque].status = "wrong";
}
}
this.changeQuestion = function(cque) {
this.currentque = this.currentque + cque;
this.displayQuiz(this.currentque);
}
}
var jsq = new quizApp();
var selectedopt;
$(document).ready(function() {
jsq.displayQuiz(0);
$('#question-options').on('change', 'input[type=radio][name=option]', function(e) {
//var radio = $(this).find('input:radio');
$(this).prop("checked", true);
selectedopt = $(this).val();
});
});
$('#next').click(function(e) {
e.preventDefault();
if(selectedopt) {
jsq.checkAnswer(selectedopt);
}
jsq.changeQuestion(1);
});
$('#previous').click(function(e) {
e.preventDefault();
if(selectedopt) {
jsq.checkAnswer(selectedopt);
}
jsq.changeQuestion(-1);
});
Also see: Tab Triggers