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.
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
© 2013 DaniWeb® LLC
Page rendered in 0.0547 seconds
using 2.69MB