1

Below I have a page design, where I have two input boxes.

Name and age.

I also have an add button.

Upon clicking the add button I have span elements with name and age enter image description here

and above will be grouped, say "friends"

like wise another group, contains a list of person objects

now i have to store data like

group 1- 
   list of objects 
   name - age
   name - age
group 2 -
   list of objects
   name - age
   name - age.

later will submit the data. I have tried var person = new Object(); , but I am looking to have key value like

name: firsname , age: 23

or is there any alternative way to achieve this..thanks in advance.

EDIT- i need to do this dynamically upon click add button

2
  • 2
    var person = {name: firstname, age: 23}; then arrayofpersons.push(person) ??? Commented Oct 31, 2013 at 17:32
  • Can you show some code that you have tried? I am confused what you are asking. Commented Oct 31, 2013 at 17:37

3 Answers 3

1

Don't need jQuery - you can just do:

var person = {
    name: firstname,
    age: 23
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

var ary = [];
var person = {};
person.name = "nimma";
person.age = 33;
ary.push(person);
person.name = "maddy";
person.age = 30;
ary.push(person);

you can also do like this:

var ary = [{"name":"nimma","age":33},{"name":"maddy","age":30}];

Fiddle here.

3 Comments

i tried, i need to persist the previous data, but this is not working, when i try to add next person object. it overrides
but it is stored in ary. you can also do it like ary = [{"name":"nimma","age":33},{"name":"maddy","age":30}];
it is working now,, i didnt declare var 'ary' as global var, after declaring global it worked.
0

You can do something like this:

var person = [];

later on when you need to insert data into the array, do this

person.push(
         {
            'name':firsname ,
            'age':23
         }
);

Hope that helps!

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.