Sunday, 15 April 2012

jquery - multidimensional object building in javascript using loops -



jquery - multidimensional object building in javascript using loops -

so have html layout in there blocks (there no prepare number of them, because can created dynamically).

in these blocks there boxes (again, can created dynamically) boxes contain *html element*s , have different data attributes

so need create object looks this

block1 = { box1 : { id : box1.data('id'), content : box1.html() }, box2 : { id : box2.data('id'), content : box2.html() } }, block2 = { box3 : { id : box3.data('id'), content : box3.html() } }

please don't write syntax not correct, know. tried somehow illustrate want.

so question how do help of jquery?

thank in advanced

you can select blocks , boxes , iterate on each of them using .each [docs]:

var blocks = {}; $('.block').each(function(index) { var boxes = {}; $(this).find('.box').each(function(index) { boxes['box' + index] = { id: $(this).data('id'); content: $(this).html(); }; }); blocks['block' + index] = boxes; });

you might not need object of objects though, maybe array of array suffices or better, depending on intend data.

to larn more how objects work, have @ mdn - working object.

javascript jquery object loops

No comments:

Post a Comment