0

I am reading a value from HTML adding 1 to that that number and then updating the number within the HTML. When I do this, it returns the number back into the HTML as NaN.

I've tried using parseInt() and Number() but I still get the same output.

HTML

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <link href="style.css" type="text/css" rel="stylesheet">
        <script type="text/javascript" src="script.js"></script>
    </head>
    <body>
        <div class="inputForm">
            <form id="frm1" onsubmit="return formSubmit()">    
                <h3>Update League Table!</h3>
                <section class="playerOne">
                    <label for="playerOne">Player One Name:</label>
                    <select name="playerOne" id="playerOne">
                        <option value="name1">name1</option>
                        <option value="name2">name2</option>
                        <option value="name3">name3</option>
                        <option value="name4">name4</option>
                    </select>
                </section>
                <br>
                <section class="playerTwo">
                    <label class="playerTwoText" for="playerTwo">Player Two Name:</label>
                    <select name="playerTwo" id="playerTwo">
                        <option value="name1">name1</option>
                        <option value="name2">name2</option>
                        <option value="name3">name3</option>
                        <option value="name4">name4</option>
                    </select>
                    <br>
                </section>
                <br>
                <section class="winner">
                    <span>Please select winner:</span>
                    <br>
                    <input id="userOne" type="radio" name="winnerWinner" value="userOne">
                    <label for="userOne">Player One</label>
                    <input id="userTwo" type="radio" name="winnerWinner" value="userTwo">
                    <label for="userTwo">Player Two</label>
                </section>
                <br>
                <section class="submission">
                    <input type="submit" value="Submit">
                </section>
                <p id="submitComplete">testing</p>
                <p id="name1GP">0</p><p id="name1Wins">0</p>

            </form>
        </div>
    </body>
</html>

JAVASCRIPT

function formSubmit() {
    var p1 = document.getElementById("playerOne").value;
    var p2 = document.getElementById("playerTwo").value;
    var radiobox = document.querySelector('input[name="winnerWinner"]:checked').value;
    if (p1 === "name1" && radiobox === "userOne") {
        var name1GP = document.getElementById("name1GP").value;
        name1GP += 1
        document.getElementById("name1GP").innerHTML = name1GP
    }
    return false;
}

The value I am reading is id="name1GP" I want to add 1 to this value and then return it to the HTML, however instead of returning 1 it returns NaN.

0

4 Answers 4

1

A paragraph element doesn't have a .value. You need to get the innerHTML as an int.

if (p1 === "name1" && radiobox === "userOne") {
    var name1GP = parseInt(document.getElementById("name1GP").innerHTML);
    name1GP += 1
    document.getElementById("name1GP").innerHTML = name1GP
}
Sign up to request clarification or add additional context in comments.

3 Comments

instead of innerHTML, I would suggest .text()
.text() would require jquery. His code is vanilla js, so I kept inline with that.
No worries @JackU, accept as answer if it helped :)
1

A p tag is not supposed to have any value property, you should probably have used var name1GP = parseInt(document.getElementById("name1GP").innerHTML, 10);

var val = document.getElementById('test').value;
console.log(val);

var val2 = document.getElementById('test').innerHTML;
console.log(val2, typeof val2);

var val3 = parseInt(document.getElementById('test').innerHTML, 10);
console.log(val3, typeof val3);
<p id="test">0</p>

Comments

0

try this one.

var name1GP = document.getElementById("name1GP").innerHtml;
name1GP = parseInt(name1GP) + 1
document.getElementById("name1GP").innerHTML = name1GP

Comments

-1

Values in HTML are rendered as strings. A string + a number will result with NaN.

change your code to parse your string into a number:

name1GP = parseInt(document.getElementById("name1GP").value)

and that should do the trick.

1 Comment

A paragraph element doesn't have .value.

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.