Charlie McShane Charlie McShane - 17 days ago 6
CSS Question

How to add an ID value to a div append

I have a "fault-counter" that stores a value of all incorrect answers. It starts at 0 and goes up one for every wrong answer.

I'm looking to have the results screen show the amount of faults you've made. So if the div #fault-counter has a value of 3, the end text will say "Sorry, you've made 3 faults!"

This is the code I've tried, but it doesn't seem to work. Any ideas on how to do this?

var faultcounter = $('#fault-counter');

$('#slickQuiz').prepend('<h1>Sorry, you have ' + $('#fault-counter') + ' faults!</h1>');


EDIT:
The HTML for the 'fault-counter' looks like this:

<div id="fault-display">
<div id="fault-counter">0</div>
<h3>FAULTS</h3>
</div>


And the jQuery being used to increase the value for every wrong answer is this:

$(".failQuestion").click(function(){
$('#fault-counter').html(function(i, val) { return val*1+1 });
});

Answer

Try this, as your fault-counter is a div than .text() should suffice.

 $('#slickQuiz').prepend('<h1>Sorry, you have ' + $('#fault-counter').text() + ' faults!</h1>');