Pass an input value to an object array

Asked

Viewed 1,048 times

2

I’m trying to pass on the value of one input text for an array of objects, via jQuery but not understood how to insert it in array. I tried to wear something like:

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

And then add in place of carlos but it didn’t work out.

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 inputs, the person fills the fields and is generated a array of objects within the variable userstocreate.

  • You can post the full HTML of the form and the full script as well?

1 answer

3


You can do it like this:

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

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

var json =  JSON.stringify(objeto);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.