How can I increment a value inside HTML?
Asked Answered
K

5

7

I'm completely new to JavaScript. I'm trying to increment a variable inside the HTML, it's not working. Is my syntax wrong?

<script>
function rps() {
computerScore.value++;
computerScore.innerHTML = computerScore.value;
}
 </script>   
<html>
<b>computer score:</b><p id="computerScore" value="0">-</p>
<button type="button" onclick="rps()">Go</button><br>
</html>
Kirsti answered 7/3, 2013 at 20:13 Comment(2)
Do not try to access DOM elements as global variables, there's a getElementById function for that.Grosmark
Even if you look for a JS solution, it is worth noting that you can handle counters with pure CSS!Philbo
M
15

value is not a valid attribute for the <p> tag.

I think you want something like:

function rps() {
    var computerScore = document.getElementById('computerScore');
    var number = computerScore.innerHTML;
    number++;
    computerScore.innerHTML = number;
}

...

<b>computer score:</b><p id="computerScore">0</p>
Marrin answered 7/3, 2013 at 20:16 Comment(1)
Actually number is a string until the ++ so perhaps number = +computerscore.innerHTML is more correctAscend
T
1

A few problems :

  • on most browsers, you can't get an element by id just as a global property
  • an attribute isn't always a property of an element (the value is only a property of input elements)

You can do this :

function rps() {
   // fetch the element :
   var element = document.getElementById('computerScore'); 

   // get the attribute, parse it and increment it :
   value = parseInt(element.getAttribute('value'), 10)+1; 

   // stores the incremented value :
   element.setAttribute('value', value);

   // and change the innerHTML (conversion to string is implicit)
   element.innerHTML = value;
}
Tryparsamide answered 7/3, 2013 at 20:17 Comment(0)
G
0

A couple issues:

  1. computerScore is nothing; you probably want document.getElementById('computerScore')
  2. value on a <p> is not valid. Use data-value or something instead (or

var computerScore = document.getElementById('computerScore');
function rps() {
    computerScore.dataset.value++;
    computerScore.innerHTML = computerScore.dataset.value;
}

Note that dataset.value is a string, but the ++ coerces it to an integer.

http://jsfiddle.net/vqPKZ/

I would also suggest using event bindings rather than event attributes, e.g. button.addEventLister

Gaius answered 7/3, 2013 at 20:19 Comment(3)
The getElementById should be called after DOMready, i.e. inside rpsGrosmark
@Ascend w3.org/TR/2011/WD-html5-20110525/…Gaius
dataset is not old IE compliant, should use getAttributeEudy
J
0
The best option will be to pass in an event object from the rps() method in the HtML,       like this: rps(event).
Then retrieve it from the Javascript method like this:
function rps(event) {
var computerScore = event.CurrentTarget.innerText;
computerScore++;
computerScore = number;
}
Joinder answered 16/5, 2017 at 17:41 Comment(2)
you're incrementing an innerText value without seeing if it's an actual number?Lida
I don't think the DOM can differentiate BTW numbers and string.Joinder
E
-3
var elm = whatever //assuming you select your element using the DOM
// update value
elm.innerHTML = +elm.innerHTML + 1;
Eudy answered 17/2, 2015 at 17:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.