Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
19
rated 0 times [  22] [ 3]  / answers: 1 / hits: 35723  / 11 Years ago, thu, march 7, 2013, 12:00:00

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>

More From » increment

 Answers
13

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>

[#79749] Thursday, March 7, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
daijac

Total Points: 568
Total Questions: 120
Total Answers: 108

Location: Virgin Islands (U.S.)
Member since Fri, May 7, 2021
3 Years ago
;