Charlie McShane Charlie McShane - 1 year ago 97
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>');

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

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

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

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

Answer Source

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

 $('#slickQuiz').prepend('<h1>Sorry, you have ' + $('#fault-counter').text() + ' faults!</h1>');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download