After following answers like this one, I’m trying to append data to a string using:
<input type='checkbox' name='a' />A
<input type='checkbox' name='b' checked='checked'/>B
<input type='checkbox' name='c' />C
var attr_str = $('input').serializeArray();
attr_str.push({q:'hello',p:'world'});
But it returns [object Object],[object Object]. What am I missing?
I’m not sure what you’re trying to accomplish but only the input which is checked is going to be included by
serializeArray(). You can read more about it here.But your array is being returned as an object as it should. To see the layout of the JSON of this object you could try
JSON.stringify(attr_str).This will show that you are appending an object into your
attr_strarray but only input ‘b’ is included because it is the only one that is checked.string=[{"name":"b","value":"on"}{"q":"hello","p":"world"}]To access this object you would simply use
attr_str[0]then for a property of that object useattr_str[0].name. Which would printb.