1

I want to make a webpage that, when you click, it adds a point. I also want that, when A checkbox is checked, each click adds 5 points. I tried this code, but it doesn't seem to be working:

    <!DOCTYPE html>
    <html>
        <head>
        <title>Click to add points!</title>
        <script type="text/javascript">
        var hey = 3;
        var points = 0;
        function addPoint(number)
        {   
        points = points + number;
        document.getElementById("points").innerHTML = points;
        };  
        function checkBox()
        {
        var chkBox = document.getElementById("extraPoints").checked;
        }
        </script>
        </head>
        <html onclick="
        checkBox();
        if (chkBox == true)
        {
        addPoint(5);
        }
        else
        {
        addPoint(1);
        }">
        <body>
        <p align="center">Points: <span id="points">0</span></p>
        <p align="center"><input type="checkbox" id="extraPoints" /></p>
        <p id="writeHere"></p>
        </body>
    </html>

I would also like to point out that I can't use jQuery. Thank you in advance.

5
  • hi doggo, how u doin Commented Apr 19, 2020 at 23:23
  • 1
    @BartekPacia lol how tf did you find this Commented Jul 30, 2020 at 23:01
  • @doggo ( ͡° ͜ʖ ͡°) Commented Jul 31, 2020 at 13:41
  • It's nice to see how far I've gotten in 3 years lol Commented Jul 31, 2020 at 15:46
  • yeah. at least you stopped using "var" Commented Jul 31, 2020 at 18:16

2 Answers 2

1

You could move the adding part inside of checkBox.

function addPoint(number) {
    points = points + number;
    document.getElementById("points").innerHTML = points;
};

function checkBox() {
    var chkBox = document.getElementById("extraPoints").checked;
    addPoint(chkBox ? 5 : 1); 
}

var hey = 3;
var points = 0;
<html onclick="checkBox();">
<p align="center">Points: <span id="points">0</span></p>
<p align="center"><input type="checkbox" id="extraPoints" /></p>
<p id="writeHere"></p>

Sign up to request clarification or add additional context in comments.

1 Comment

You helped me a lot! Thank you!
1

Use following approach.

Note: You can adjust the added points as you wish.

var points = 0, //initial value 
    elem = document.getElementById('points'), //get span holding the value
    box = document.getElementById('extraPoints'); //get the checkbox

    document.addEventListener('click', function(){ //add click event on whole document
       box.checked ? points += 5 : points += 1 //if checkbox checked, add +5 - if not, add +1
       elem.innerHTML = points; //actualize the value in the span
    });
<p align="center">Points: <span id="points">0</span></p>
<p align="center"><input type="checkbox" id="extraPoints" /></p>
<p id="writeHere"></p>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.