2

I'm trying to make a simple rating system using php and javascript . the idea is to change the value of HTML div (number of rate) using javascript without refreshing the page , the problem is that i can't get the value of the HTML div . this is the div :

<div id="rating" >5565</div> 

and i try to get this value using javascript

function give_id_like() {
    var a = document.getElementById("rating").value;
    var c = a + 1;
    document.getElementById("rating").innerHTML = c;
}

but i get this result, for example if i have in the div 88 when i run the function i get this 881 if i run it again i get 8811 . what i want is when i run the function i want to get 89 and when i run it again 90 and so on .

Blazemonger
  • 90,923
  • 26
  • 142
  • 180
didyeg
  • 35
  • 5

2 Answers2

5

This happens because value property returns String object. You need parseInt for this:

c = parseInt( a, 10 ) + 1;
antyrat
  • 27,479
  • 9
  • 75
  • 76
  • @didyeg because you need to use `innerHTML`, not `value` in `document.getElementById("rating").value` – antyrat Aug 15 '14 at 13:51
1

You need to use parseInt()

function give_id_like() {
    var a = parseInt(document.getElementById("rating").value);
    var c = a + 1;
    document.getElementById("rating").innerHTML = c;
}
monkeyinsight
  • 4,719
  • 1
  • 20
  • 28