Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding to a variable with a function JavaScript in HTML

I've only started learning Javascript for a few hours, and am trying to use a function to add to a variable using JS within HTML (Sorry if I used the wrong terminology) and it doesn't seem to be changing the result.

<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script>
            var userID = 1;
            var userRep = 0;
            function NumUp(userRep) {
                userRep = userRep + 1;
            }
            function NumDown(userRep) {
                userRep = userRep - 1;
            }
            function myFunction() {
                document.getElementById("demo").innerHTML = userRep;
            }
        </script>
    </head>
    <body>
        <form action="Reputation.JS">
            <div class="container">
                <p>result goes here</p>
                <button type="button" onclick="NumUp()" id="voteUp">Vote Up</button>
                <button type="button" onclick="NumDown()" id="voteDown">Vote Down</button>
                <label id="lbl"></label>
                <button onclick="myFunction()">Click me</button>
                <p id="demo"></p>
            </div>
        </form>
    </body>
</html>

Much thanks for any help you can give.

like image 239
Ryan Johnston Avatar asked Dec 06 '22 12:12

Ryan Johnston


1 Answers

I hope this will help. Thank you.

<html>
<head>
    <title>TODO supply a title</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script>
        var userID = 1; 
        var userRep = 0;
        function NumUp(){
            userRep = userRep + 1;
            myFunction();
        }
        function NumDown(){
            userRep = userRep - 1;
            myFunction();
        }

        function myFunction() {
        document.getElementById("demo").innerHTML = userRep;
        }
    </script>

</head>
<body>

<form action="Reputation.JS">
        <div class="container">
            <p>result goes here</p>
            <button type ="button" onclick="NumUp()"  id="voteUp">Vote Up</button> 
            <button type ="button" onclick="NumDown()"  id="voteDown">Vote Down</button>
            <label id="lbl"></label>
            <button onclick="myFunction()">Click me</button>
            <p id="demo"></p>

        </div>
    </form>

</body>
like image 89
Alexis Avatar answered Dec 17 '22 10:12

Alexis