hi, i am have made a button, and everytime i click it it takes a value from my input box, and it subtracts it and stores it back into the input box value. but what i am trying to do is, store my value into an array or anything everything my button is clicked.

so for example i want to do this,
balance = 100
button is clicked
balance is now 90
and i want
array[0] to have 100
array[1] to have 90
and everytime i want it to continue to append to the array but when i enter my button.

i have tried many things and everytime it always stores the value into array [0] and over writes it

Recommended Answers

All 8 Replies

Create a other variable that stores the click count. Every time you click, add o e to that variable. For example...


Then reference this variable for the array index...

 array[clickTotal] = value;

Do you have existing code that you can show?

is the clickTotal++ a built in function using local storage?
or just something like

var clicktotal=0

is the clickTotal++ a built in function using local storage?

Neither. The idea is to declare the variable in your script block to simply hold the total number of clicks. Use that variable for the position in your array.

var clicktotal=0;
var array = [];

function myfunc(){

var Bal = document.getElementById("Bank_Balance");
var Balance = Bal.value
console.log(clicktotal+"My array click total "+array[clicktotal]);

see i have this and there are some other things in the function, but the console.log is alway giving me the same number without incrimenting my variable, so it is still overwriting my array.

where my button looks like this

<input type="button" id="buy_things" value="Buy" style="width:55px; height:33px; color:#938CDD;" >

and my init functiong is

var Buy_button = document.getElementById("buy_things");
                Buy_button.addEventListener ('click', myfunc ,true);

I didnt test out your code, but your clicktotal++; should be inside of the function so that each time the function executes, the clicktotal variable increases by one.

If you put your total code on jsfiddle.net, we can work easier.

its ok thanks, i got it working usign array push

Your logic is a little off.
If your input box is 100 and your subtract that value from itself - that will be zero.
There needs to be a Balance field as well as a field that is containing the value to subtract from the Balance.

Be a part of the DaniWeb community

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