Thanks for the response mkmanning!

So far that is similar to the solution I am using (with a small change
of using html() instead of append() for the main div and no need for
appendTo() since the main div is already part of the DOM). I was just
wondering if there was an obvious shortcut with JQuery for doing
something like this (taking an array of objects and turning them into
content with data or event info to attach to those elements)... but
looks like that may be the best bet then.

Thanks again!

On Mar 23, 11:59 pm, mkmanning <michaell...@gmail.com> wrote:
> Why not do two passes, since the array will have a one-to-one
> correspondence with the divs in the containing div?
>
> $(document).ready(function(){
>     var arr = [{content: 'some html 1', data: 'some data 1'},{content:
> 'some html 2', data: 'some data 2'},{content: 'some html N', data:
> 'some data N'}];
>     $('<div>').append($.map(arr,function(a){return '<div>'+a.content
> +'</div>';}).join('')).children('div').each(function(i,a){$(a).data
> ('testValue',arr[i].data)}).end().appendTo('body');
>
> });
>
> On Mar 23, 8:26 pm, sliver <sliver2...@gmail.com> wrote:
>
> > Hi all,
>
> > Ive been exploring various solutions for the following and was
> > interested to hear other opinions:
>
> > From AJAX call: "[{content: 'some html 1', data: 'some data 1'},
> > {content: 'some html 2', data: 'some data 2'}, ... , {content: 'some
> > html N', data: 'some data N'}]"
>
> > Desired: Take the array from above, wrap the content values in a div
> > and then attach the data to the div. After this, set the new divs to
> > be the content of another element.
>
> > My first thought was to put the array through a map:
> > $.map(ARRAY, function(a) {
> >         return $('<div>').html(a.content).data('testValue', a,data);
>
> > });
>
> > however, you can't simply just take the output from that and use it as
> > an argument in another element's html method:
> > // Won't work
> > $('<div>').html($.map(ARRAY, function(a) { return $('<div>').html
> > (a.content).data('testValue', a,data);}));
>
> > Ive been working through a few solutions, none of which I find to be
> > very elegant... so, any thoughts on an elegant solution out there?

Reply via email to