Javascript, saving values from onclick and saving previous values in arrays -


i'm trying save values retrieved "onclick" , save values retrieved "onclick" in different arrays when buttons clicked.

but seems when "for loop" used, newly retrieved values overwrite retrieved data though values saved separately in different arrays.

i'm confused right now, know why?

(if hit button "refresh", can see current values saved.)

var firstvalue = [];  var prevalue = [];        function returned(a){        prevalue = firstvalue;      console.log("returned! prevalue:  "+prevalue);        (var = 0; < 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>

in javascript arrays objects. when assign: prevalue = firstvalue; takes reference variable hence fail copy value.

in order achieve that, need copy each value of array previous values array. so, can run loop first previous assignment:

for (var = 0; < 1; i++) {   prevvalue[i] = firstvalue[i]; } 

assuming of same size.


Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -