1,105,310 Community Members

JQuery adding variables and outputting onto page

Member Avatar
jaylb
Newbie Poster
1 post since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.
Member Avatar
niranga
Junior Poster
197 posts since Apr 2010
Reputation Points: 9 [?]
Q&As Helped to Solve: 27 [?]
Skill Endorsements: 0 [?]
 
0
 

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

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: