0

i'm trying to save values retrieved from "onclick" and also save the values previously retrieved from "onclick" in different arrays when buttons are clicked.

but it seems like when "for loop" is used, newly retrieved values overwrite previously retrieved data even though those values are saved separately in different arrays.

i'm really confused right now, does anybody know why?

(if you hit the button "refresh", you can see the current values that are saved.)

var firstValue = [];
var preValue = [];



function returneD(a){

    preValue = firstValue;
    console.log("returned! preValue:  "+preValue);

    for (var i = 0; i < 1; i++) {
      firstValue[i] = a;
      console.log("returned! firstValue:  "+firstValue);
    }
}


function refresh1(){
  console.log("preValue:  "+preValue);
  console.log("firstValue:  "+firstValue);
}
<!DOCTYPE html>
<html>
<head>
  <script src="jstest.js"></script>
</head>
<body id="thebody">

<button id="1" onclick="returneD(this.id)">O N E</button>
<button id="2" onclick="returneD(this.id)">T W O</button>
<button id="3" onclick="returneD(this.id)">T H R E E</button>
<button id="4" onclick="returneD(this.id)">F O U R</button>
<br>
<button onclick="refresh1()">refresh</button>

</body>
</html>

1
  • preValue = firstValue; doesn't create a copy; it only causes preValue to point at the same array in memory. Try this instead: preValue = firstValue.splice(); Commented Mar 28, 2017 at 12:17

2 Answers 2

2

Please refer this code will work,

firstValue is array which already stored value after that this will assign old value to preValue array.

var firstValue = [];
var preValue = [];



function returneD(a){

    preValue = a;
    console.log("returned! preValue:  "+preValue);

    for (var i = 0; i < 1; i++) {
      firstValue[i] = a;
      console.log("returned! firstValue:  "+firstValue);
    }
}


function refresh1(){
  console.log("preValue:  "+preValue);
  console.log("firstValue:  "+firstValue);
}
<!DOCTYPE html>
<html>
<head>
  <script src="jstest.js"></script>
</head>
<body id="thebody">

<button id="1" onclick="returneD(this.id)">O N E</button>
<button id="2" onclick="returneD(this.id)">T W O</button>
<button id="3" onclick="returneD(this.id)">T H R E E</button>
<button id="4" onclick="returneD(this.id)">F O U R</button>
<br>
<button onclick="refresh1()">refresh</button>

</body>
</html>

Sign up to request clarification or add additional context in comments.

Comments

1

In JavaScript Arrays are just like Objects. When you assign: preValue = firstValue; this simply takes a reference to the variable hence you fail to copy the value.

In order to achieve that, you need to copy each value of that array to the previous values array. So, you can run another for loop for first to previous assignment:

for (var i = 0; i < 1; i++) {
  prevValue[i] = firstValue[i];
}

Assuming they are of the same size.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.