Reputation: 11
HI, i have an object: var myobject = new Object; and i want to dynamically fill it with properties while looping through jquery input collection in that manner:
$('.test').each(function(){
myobject.$(this).attr('name') = $(this).val();
});
what i'm doing wrong here? thanks in advance
Upvotes: 1
Views: 1536
Reputation: 30996
Try this:
$('.test').each(function () {
var e = $(this);
myobject[e.attr('name')] = e.val();
});
Objects in JavaScript can be accessed using object.property
or object['property']
(these two are equivalent). The latter allows you to use expressions (like variables): object[propertyName]
.
Upvotes: 4
Reputation: 207501
With the way you are doing it:
var myObject = {};
$('.test').each(
function(){
var elem = $(this);
myObject[elem.attr('name')] = elem.val();
}
);
Upvotes: 0