Andrew Nguyen
Andrew Nguyen

Reputation: 1436

Incrementing value based on number of clicks

Objective

Problem

scripts.js

function countCorrect() {
    var count = 0;

    $(".is-correct").on("click", function(){
        count = count++;
        console.log("Answer is correct.");
        console.log(count); // Expect an integer from 1-10
    });

    return count;
}

function changeTotalCorrect(func) {
    var count = countCorrect(); // Expect an integer from 1-10
    $(".highlight").html(count); // Change the HTML to reflect the new number
}

changeTotalCorrect();

index.html

<div class="buttons">
                    <button class="btn btn--option btn--one is-correct">${{ content.price_correct|int }}</button>
                    <button class="btn btn--option btn--two">${{ content.price_incorrect_one|int }}</button>
                    <button class="btn btn--option btn--three">${{ content.price_incorrect_two|int }}</button>
                    <button class="btn btn--option btn--four">${{ content.price_incorrect_three|int }}</button>
                </div>

Upvotes: 0

Views: 114

Answers (3)

Shiladitya
Shiladitya

Reputation: 12181

Here you go with a solution using jQuery https://jsfiddle.net/jeogdzh7/

var cnt = 0;

$('button').each(function(){
   $(this).attr('cnt', cnt);
})

$('button').click(function(){
   if($(this).hasClass('is-correct')){
      $(this).html( "Count " + (parseInt($(this).attr('cnt')) + 1 ));
      $(this).attr('cnt', (parseInt($(this).attr('cnt')) + 1));
   }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="buttons">
  <button class="btn btn--option btn--one is-correct">Count 0</button>
  <button class="btn btn--option btn--two">Count 0</button>
  <button class="btn btn--option btn--three">Count 0</button>
  <button class="btn btn--option btn--four">Count 0</button>
</div>

I'm using attribute to store the count of click. .hasClass is being used for checking the class present in button.

Rather than having two individual function to update the count, I've done it using within click function.

Updated Code

$('button').click(function(){
   if($(this).hasClass('is-correct')){
      $(this)
        .attr('cnt', (parseInt($(this).attr('cnt')) + 1))
        .html( "Count " + $(this).attr('cnt'));
   }
 });

Hope this will help you.

Upvotes: 0

Manuel Otto
Manuel Otto

Reputation: 6540

You have to put the click listener outside of the function like this:

var count = 0;

$(".is-correct").on("click", function(){
    count++;
    console.log("Answer is correct.");
    console.log(count); // Expect an integer from 1-10
    changeTotalCorrect();
});


function changeTotalCorrect(func) {
    $(".highlight").html(count); // Change the HTML to reflect the new number
}

changeTotalCorrect();

var count = 0;

$(".is-correct").on("click", function(){
    count++;
    console.log("Answer is correct.");
    console.log(count); // Expect an integer from 1-10
    changeTotalCorrect();
});


function changeTotalCorrect(func) {
    $(".highlight").html(count); // Change the HTML to reflect the new number
}

changeTotalCorrect();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="buttons">
                    <button class="btn btn--option btn--one is-correct">${{ content.price_correct|int }}</button>
                    <button class="btn btn--option btn--two">${{ content.price_incorrect_one|int }}</button>
                    <button class="btn btn--option btn--three">${{ content.price_incorrect_two|int }}</button>
                    <button class="btn btn--option btn--four">${{ content.price_incorrect_three|int }}</button>
                </div>

Upvotes: 3

ivo
ivo

Reputation: 1113

The problem is

count = count++;

It should be:

count++;

Using the ++ operator after a variable works by evaluating to the current value (0 in this case) and then incrementing the value stored in the variable. It's the evaluated value (0) that you were incorrectly assigning back to the variable.

Upvotes: 0

Related Questions