I know its fundamental but got stuck. I'm trying to add objects into an array:
Html
<p>
<label>
<input type="text" name="test1" id="test1" />
</label>
</p>
<p>
<label>
<input type="text" name="test2" id="test2" />
</label>
</p>
Script
var res = {};
var array_res = [];
function init(){
x= $('#test1').val();
res['x'] = x;
y= $('#test2').val();
res['y'] = y;
array_res.push(res);
return array_res
}
$('#btn').click(function(){
init();
console.log(array_res);
});
When first adding x=1 and y=2 i get at console:
[Object { x="1", y="2"}]
Next i try to insert x=3 and y=4 and i get:
[Object { x="3", y="4"}, Object { x="3", y="4"}]
instead of
[Object { x="1", y="2"}, Object { x="3", y="4"}]
Why is that? I smell its an object instance issue but can't figure out what to do. Thanks in advance
var res = {}into theinitfunction.