I am trying to build an array containing all the values of every form element. The alert works just find, but on console.log data is empty, am I failing to understand how scoping in javascript works?
var data = new Array();
$("#page2 input").each(function(index) {
alert($(this).attr('id'));
data[$(this).attr('id')] = $(this).val();
});
Many thanks for your time,
One thing to know is that an
Arrayin JavaScript can only ever have positive integer indexes (which are really treated as strings).When you assign a string to the key, you are assigning properties to an
Object.You probably want something like this…
jsFiddle.
If you wanted just the
idattributes.jsFiddle.