Добрый день!
Есть объект item и массив arr. Не могу понять, почему при изменении объекта (в момент выполнения item.description = $(this).val();) изменяются и все элементы массива, все они становятся равны новому значению item.
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<script src="http://code.jquery.com/jquery-2.2.0.min.js"></script><style type="text/css"></style>
<script>
$(document).ready(function() {
var item = {
description: ""
};
arr = [];
//если поле input с id="description" изменилось, меняем item.description
$('#description').change(function(){
item.description = $(this).val();
}
});
// при нажатии на кнопку Добавить добавляем новый элемент в массив
$('#add').click(function() {
arr.push(item);
});
});
</script>
</br>
<input id="description" name="description" type="text">
<input id="add" type="submit" value="Добавить"/>