Question: I have a form

```
<input type="text" id='name'>
<input type="text" id='place'>
<button id='add'>Add</button>        
<button id='show'>Show</button>

```

I have a n object to collect the elements when Add button is triggered and the code is 

```
var measure = {};
$("#add").click(function(){
    measurecol = new Object();
    measurecol.name = $("#name").val();
    measurecol.place = $("#place").val();
    measure[measurecol.name] = measurecol;
});

```

Whe I refresh my application, first it shows the elements of the object when no action is performed in the Add button:

```
{ 'foo': { 'name':'foo', 'place': 'foo1' } }

```

But when I fill the form and trigger Add button, the old element in the object gets deleted and the new one is added in the object. This happens only on refreshing the application.

So, my question is how to retrieve the element of the object upon refreshing the application and upon adding an element, it should add with the old one:

```
{
  'foo': {
    'name': 'foo',
    'place': 'foo1'
   },
   'goo': {
     'name': 'goo',
     'place': 'goo1'
   }
}

```

Answer: 
first you can check whether the object is empty or not. If its empty, add the element in the object else if its not empty, append the element based on the length of the object.

```
if(measure != undefined && measure !={}){
    measure[measure.length] = measurecol;
}else{
    measure[measure.length+1] = measurecol;
}

```

hope it helps
Using `localStorage`:

```
var measure = {};
$("#add").click(function(){
    measurecol = new Object();
    measurecol.name = $("#name").val();
    measurecol.place = $("#place").val();
    measure[measurecol.name] = measurecol;
    var lastobject =  localStorage.getItem("lastobj");

    if(lastobject){
        lastobject = JSON.parse(lastobject);
        var l = Object.keys(lastobject).length
        lastobject['obj'+l]=measurecol;
    }else{
        lastobject = new Object();
        lastobject.obj0 = measurecol;
    }
    localStorage.setItem("lastobj", JSON.stringify(lastobject));
});

```