Hi,

I have javascript which counts seperately every time 2 seperate buttons are clicked, this then outputs the amount on the same page. This works, however I am now trying to add a third value to output on the page which is the total of the clicks which i would calculate by adding the 2 attributes together. This just doesn't seem to be working though.

java

<script type="text/javascript">
var NextClick = 0;
var PrevClick = 0;
var TotalClicks = 0;

function getValueNext()
  {
  var y=document.getElementById("NextClick").value = ++NextClick
  $('.NextClick').val(y);
  }

function getValuePrev()
  {
  var x=document.getElementById("PrevClick").value = ++PrevClick
  $('.PrevClick').val(x);
  }

function getSumClicks()
  {
  var z= x + y
  $('.TotalClicks').val(z);
  }

</script>

Html

Clicked next <input class="NextClick" size="3" onfocus="this.blur();" value="0"> times.
  Clicked prev <input class="PrevClick" size="3" onfocus="this.blur();" value="0"> times.
  Clicked total <input class="TotalClicks" size="3" onfocus="this.blur();" value="0"> times.

What were you expecting from these two lines?

var y=document.getElementById("NextClick").value = ++NextClick
$('.NextClick').val(y);

There aren't any element with an ID NextClick
$('.NextClick').val(y) is correct

I believe what you were expecting is increasing the NextClick value by 1 and show it on the text field

function getValueNext()
{
    $('.NextClick').val(++NextClick);
}

Do the same changes to the other method too

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.