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>
isherwood
  • 58,414
  • 16
  • 114
  • 157
pasha_codes
  • 625
  • 2
  • 5
  • 19
  • Do not try to [access DOM elements as global variables](http://stackoverflow.com/q/3434278/1048572), there's a [`getElementById`](https://developer.mozilla.org/en-US/docs/DOM/document.getElementById) function for that. – Bergi Mar 07 '13 at 20:16
  • 1
    Even if you look for a JS solution, it is worth noting that you can [handle counters with pure CSS](https://www.w3schools.com/cssref/pr_gen_counter-increment.asp)! – Frank Zalkow Dec 18 '17 at 17:26

5 Answers5

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>
Adam Plocher
  • 13,994
  • 6
  • 46
  • 79
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;
}
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
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

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
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;
}
Misterwyz
  • 101
  • 1
  • 2
  • 6
-3
var elm = whatever //assuming you select your element using the DOM
// update value
elm.innerHTML = +elm.innerHTML + 1;
vsync
  • 118,978
  • 58
  • 307
  • 400