As outlined in this question and answer, JQuery objects can be created with attributes in place. For instance:
jQuery('<div/>', {
id: 'foo',
href: 'http://google.com',
title: 'Become a Googler',
rel: 'external',
text: 'Go to Google!'
}).appendTo('#mySelector');
As a part of this, one can add the data field, so that you have:
jQuery('<div/>', {
id: 'foo',
data: ('meta', 'my-meta-data'),
href: 'http://google.com',
title: 'Become a Googler',
rel: 'external',
text: 'Go to Google!'
}).appendTo('#mySelector');
which will create the data field data-meta='my-meta-data'
within the div 'foo'.
Now, if I have an object
var my_data = {
meta1: 'my-meta-data-1',
meta2: 'my-meta-data-2'
}
how would I go about using that object in the data field when creating my JQuery object? It would great if I could just say
jQuery('<div/>', {
id: 'foo',
data: my_data,
href: 'http://google.com',
title: 'Become a Googler',
rel: 'external',
text: 'Go to Google!'
}).appendTo('#mySelector');
but that would be too easy.
NOTE: I though the data: (key, value)
worked but I have been trying it and it does not appear to be. My question, despite that, still stands.