I'm trying to create a JSON tree out of an each loop, that get information from several id's on the page. Consider that each id has names like "textHolder1, textHolder2" etc.
$('[id^="textHolder"]').each(function(){
TextElementID = this.id;
TextElementContent = $(this).text();
TextElementClass = $(this).attr('class');
TextElementStyle = $(this).attr('style');
json = JSON.stringify({textElements:{TextElementID:{textContent:TextElementContent,textClass:TextElementClass, textStyle: TextElementStyle}}}, null, 4);
});
alert(json)
And I'm trying to get this output in JSON:
{
"textElements": {
"textHolder1": {
"textContent": "The content",
"textClass": "theclass",
"textStyle": "some-styles"
}
"textHolder2": {
"textContent": "The content1",
"textClass": "theclass2",
"textStyle": "some-styles2"
}
}
}
How is this done, and what am I doing wrong?