Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Laurence kite
11,768 PointsMore problems with document.write and JavaScript
Hi I have voiced some of this before but can not get it to work. I am attempting the ultimate quiz challenge in Javascript basics.
I can not see how to use the document.write statement to append to a website. it seems to replace all the html code before hand and means there is no point putting it there as it gets overwritten.. I was advised to use the onLoad event in the body to only load the script after the page has been loaded but this does not improve matters.
Here is my code:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <link rel="stylesheet" href="css/main.css"> <title>The Ultimate Quiz Challenge</title> </head> <body onload="askQuestions();"> <div class="container"> <h1>The Ultimate Quiz Challenge</h1> <h3>Welcome to the ultimate quiz challenge</h3> <p>I am going to asking a number of questions and rate your performance</p> </div> <script src="quiz.js"></script> </body> </html>
Here is the script:
function askQuestions() { var question1 ="<p>What is the capital of England</p>"; var firstanswer ="London"; var question2 = "<p>How many sides are there to a square</p>"; var secondanswer = 4; var noofquestions = 2; var count = 1 while (count <= 2) { var temp = eval('question' + count); document.write(temp); /*var answer = prompt("Please type your answer "); */ count++; } }
The problems is that the questions are written to the page but all the previous html such as the title the welcome to the quiz message is written over as soon as the page is loaded.
Another problem is if I take out the comments surrounding the prompt line, then the prompt appears on its on before the questions are displayed?