I am trying to store an object as an attribute in another object but can't seem to access it. Is it possible?
<script language="javascript">
$(document).ready(function() {
//create a TestObject
function TestObject() {
this.testProperty = "green";
}
//and an instance of it
var testObject = new TestObject();
//attach this instance to the div as a property
//if it could be attached another way perhaps that's the answer??
$('#test').attr("obj", testObject);
alert(testObject.testProperty);//works - obviously
alert($('#test').attr("obj").testProperty); //does not work
var o = $('#test').attr("obj");
alert(o.testProperty); //does not work
});
</script>
<body>
<form id="form1" runat="server">
<div id="test">Here is a test div</div>
</form>
</body>
Answer: The guys below had it right
$(document).ready(function() {
//create a TestObject
function TestObject() {
this.testProperty = "green";
}
//and an instance of it
var testObject = new TestObject();
//attach this instance to the div as a property
var test;
test = $('#test');
jQuery.data(test, "obj", testObject);
alert(testObject.testProperty); //works - obviously
alert(jQuery.data(test, "obj").testProperty); //works!!
});