I am trying to build a DOM with jQuery and populate it with data obtained using AJAX (data type = json). I would also like to save this data as an object attached to a specific DOM element. Does jQuery provide any method for this? The reason I want to do this is because only part of the data is initially displayed; other data may be needed later, depending on the user's actions.
I tried using attr() , but it saves the string “Object Object” instead of the actual object:
var div = $('<div/>'); div.attr('foo', {bar: 'foobar'}); alert(div.attr('foo')); // gives "[object Object]" alert(typeof div.attr('foo')); // gives "string" alert(div.attr('foo').bar); // gives "undefined"
Another way to do this would be to jQuery's "workaround" ( div[0].foo = {bar: 'foobar'}; ), although this seems like a "dirty workaround" if jQuery already supports object binding.
Any ideas? Thanks in advance!
jquery
binaryLV
source share