The assignment of the "click" handler for "knapsvar" should be done like this:
$('body').on('click', '#knapsvar', function() {
event.preventDefault();
// and so on
}
If you do it that way, you fix the current issue: every time you overwrite the HTML for the questions/answers, you're getting rid of the event handler assignment. In other words, the code you have now sets up the "click" handler directly on the element you created. When you create the next question, you eliminate that element, and with it goes the event handler assignment.
By using the delegated form above, the handler goes on the <body>
element. The handler responds to clicks on any target element whose id is "knapsvar", so it won't matter that you've generated new HTML for each question.
This isn't really on the original topic, but you could improve the data structure used for your question/answer list to make creating the HTML a lot easier.
var spg = [{
"question": "Hvilke farver har det BRASILIANSKE flag?",
answers: [
"Rød, Grøn, Gul",
"Gul, Grøn, Blå",
"Hvid, Grøn, Rød",
"Blå, Rød, Hvid"
],
"correct": 1
}, {
"question": "Hvilke farver har det ITALIENSKE flag?",
answers: [
"Gul, Grøn, Rød",
"Blå, Grøn, Gul",
"Rød, Grøn, Hvid",
"Hvid, Rød, Blå"
],
"correct": 2
}, {
"question": "Hvilke farver har det AUSTRALSKE flag?",
answers: [
"Grøn, Gul, Rød",
"Grøn, Gul, Blå",
"Grøn, Hvid, Rød",
"Rød, Blå, Hvid"
],
"correct": 3
}, ];
Now, to build the HTML from that:
function answers() {
$("#a").html(
'Svarmuligheder: <br>' +
spg[current].answers.map(function(ans, index) {
return "<input type=radio name=a id=" + index + (class == spg[current].correct ? " class=correct " : "") + "> " +
ans + "<br>";
}).join("") +
'<input id="knapsvar" type="submit" value="Svar"><input id="continue" type="submit" value="fortsæt"><input id="forfra" type="submit" value="forfra">'
);
Now the code that checks to see if the answer is correct can just check to see if the checked radio button has the class "correct".
Note that people taking the quiz can quite easily examine the page source and cheat, and that's true no matter how you write the JavaScript code. The only way to make it secure is to check the answers at the server.