Reputation: 1428
Greeting
Нow to add the associative array to an jquery object?
examples:
<div id = "element"></div>
$('#element').attr('_mynumber', 10);
console.log($('#element').attr('_mynumber'); // OK, _mynumber = 10
$('#element').attr('_myarray', [1,2,3,9,8,7]);
console.log($('#element').attr('_myarray'); // OK, _myarray = 1,2,3,9,8,7
$('#element').attr('_myassoc', {val1: 10, val2: [1,2,3,9,8,7]});
console.log($('#element').attr('_myassoc'); // FAIL, _mynumber = [object Object] (only as string), _mynumber.val1 = undefined
Upvotes: 0
Views: 124
Reputation: 12452
Use data
to store additional information on an element instead of attr
.
$('#element').data('_myassoc', {val1: 10, val2: [1,2,3,9,8,7]});
console.log($('#element').data('_myassoc'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="element"></div>
Upvotes: 2