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>
                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>
                        A few problems :
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;
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With