Help me with javascript?

Deven

New Member
Okay, so, I want to add text like I did in the html with the <p> tags, But I have no idea how to get text into a function in JavaScript. I want to put text above each if statement, how?

My code:
Code:
<html>
<head>
<style>
body {margin-top:50px;}
</style>
</head>
<body>
<center>

<p>2 + 2 =?</p>
<p><input id="answer" type="text"></p>
<p>2 + 3 = ?</p>
<p><input id="answer2" type="text2"></p>
<p>5 + 5 = ?</p>
<p><input id="answer3" type="text3"></p>
<p>3 + 3 = ?</p>
<p><input id="answer4" type="text4"></p>
<p>6 + 5 = ?</p>
<p><input id="answer5" type="text5"></p>
<p>6 + 12 = ?</p>
<p><input id="answer6" type="text6"></p>

<script language = "javascript">
var answers =
[
"4",
"5",
"10",
"6",
"11",
"18"
];

var questions = function submit() {
var text = document.getElementById("answer").value;
var text2 = document.getElementById("answer2").value…
var text3 = document.getElementById("answer3").value…
var text4 = document.getElementById("answer4").value…
var text5 = document.getElementById("answer5").value…
var text6 = document.getElementById("answer6").value…
document.getElementById('div1').inner… = "2+2=?";
if(text === answers[0])
{
document.write("Correct!");
}
else{document.write("Incorrect, the correct answer is 4")}
document.write("<p>")

if(text2 === answers[1])
{
document.write("Correct!");
}
else{document.write("Incorrect, the correct answer is 5")}
document.write("<p>")

if(text3 === answers[2])
{
document.write("Correct!");
}
else{document.write("Incorrect, the correct answer is 10")}
document.write("<p>")

if(text4 === answers[3])
{
document.write("Correct!");
}
else{document.write("Incorrect, the correct answer is 6")}
document.write("<p>")

if(text5 === answers[4])
{
document.write("Correct!");
}
else{document.write("Incorrect, the correct answer is 11")}
document.write("<p>")

if(text6 === answers[5])
{
document.write("Correct!");
}
else{document.write("Incorrect, the correct answer is 18")}

document.write("<p>")
}
</script>

<button onclick="submit()"> show results </button></p>
</script>

</center>
</body>
</html>
 
Last edited by a moderator:

ronaldroe

Super Moderator
Staff member
Code:
document.getElementById("answer").insertAdjacentHTML('afterend', 'Correct!');

Also, wrap your code in
Code:
 tags
 
Top