user6370676
user6370676

Reputation:

javascript returns NaN in additition

I am trying to calculate total number of links click by user. To do so i am using following code

<html>
<head>
  <script type="text/javascript">
   function fnc()
    {
    document.getElementById("atext").innerHTML="tested";
    var iStronglyAgreeCount=parseInt (document.getElementById("ISA") );
    document.getElementById("ISA").innerHTML=iStronglyAgreeCount +1;
    }
       </script>
    </head>
  <body>
 <a href="#">  <label id="atext" onClick="fnc()">I strongly   agree</label></a> (<span><label id="ISA">0</label></span>) 

</body>

I am storing starting number 0 into a variable and trying to add 1 at each click.But it shows NaN.

Upvotes: 0

Views: 57

Answers (1)

Rayon
Rayon

Reputation: 36599

Use .textContent to get the text content of the element.

function fnc() {
  document.getElementById("atext").innerHTML = "tested";
  var iStronglyAgreeCount = parseInt(document.getElementById("ISA").textContent);
  document.getElementById("ISA").innerHTML = iStronglyAgreeCount + 1;
}
<a href="#">
  <label id="atext" onClick="fnc()">I strongly agree</label>
</a>(<span><label id="ISA">0</label></span>)

Note: If target browser is <IE9, consider using Polyfill

Upvotes: 3

Related Questions