Pass value of an input to an array of objects

2

I am trying to pass the value of an input text to an array of objects, via jQuery but I did not understand how to insert it into array . I tried to use something like:

var usuario = $('#username').val(); 

And then add in place of carlos but did not work.

Nome:<input type="text" id="username">
Senha:<input type="password" id="password">

var userstocreate = [{ 
                        username: 'carlos',
                        password : 'Carlos123@'
                    }];

In short, I needed to create a JSON based on the values of the input s, the person fills the fields and an array is generated within the userstocreate variable.

    
asked by anonymous 12.03.2015 / 18:49

1 answer

3

You can do this:

var objeto= new Object();
objeto.nome = $('#username').val();
objeto.senha =  $('#password').val();

And if you need to create a JSON you just have to serialize the object.

var json =  JSON.stringify(objeto);
    
12.03.2015 / 19:02