If I have the following DOM elements
content1
content2
how, usi
var data = $('div.item').map(function(){
return {
classname: 'item',
content: $(this).text()
};
}).get();
DEMO: http://jsfiddle.net/nDE7e/
http://jsfiddle.net/24JjD/
var datas = [{
'classname': 'item',
'content': 'content1'
}, {
'classname': 'item',
'content': 'content2'
}
];
$.each(datas, function(key, value) {
$('body').append('<div class="'+value.classname+'">'+value.content+'</div>');
});
Correct answer :
http://jsfiddle.net/tS9r5/
var datas = [];
$('div.item').each(function() {
var data = {
classname: this.className,
content: $(this).text()
};
datas.push(data);
});
console.log(datas);
You can loop over the item divs and add objects to an array.
var items = new Array();
$("div.item").each(function() {
var item = {classname: this.className, content: $(this).text()};
items.push(item);
});