We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,708 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

JQuery adding variables and outputting onto page

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.
2
Contributors
1
Reply
17 Hours
Discussion Span
1 Year Ago
Last Updated
2
Views
jaylb
Newbie Poster
1 post since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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

niranga
Junior Poster
192 posts since Apr 2010
Reputation Points: 21
Solved Threads: 26
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0547 seconds using 2.69MB