Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If it's going to be on the element only, store the data on the element using the "data()" method:</p> <pre><code>function loadList(){ $.get("myservice",function(data){ $('.myElement').data('svc-data',data); } } </code></pre> <p>Then, when you need it in your mouseover, just retrieve it using the same data method:</p> <pre><code>$('.myelement').on('mouseover',function() { var data = $(this).data('svc-data'); alert(data.list[2].key1); }); </code></pre> <p>Doc on Data: <a href="http://api.jquery.com/category/miscellaneous/data-storage/" rel="nofollow">http://api.jquery.com/category/miscellaneous/data-storage/</a></p> <p><strong>Update:</strong> further clarification</p> <p>'svc-data' is a "key" you give your data on the element. The data method stores data (objects, json, strings, etc...) by associating it with the element specified using the key for reference. So, 'svc-data' is what I called that reference since it was data from your service (svc = service to me). If you save JSON data (ie: list.sub.item) you will still be able to access it as JSON when you retrieve it since jQuery stores the object you give as the same type - no serialization is used. </p> <p>The second part shows how to retrieve the data associated with an element. When you retrieve using $('selector').data('key') (see above) the data comes back as the type you saved and can be used as such. In the case of JSON, you retrieve a JSON object and can still use it as such (ie: list.sub.item) as I tried to show with the alert. </p> <p>The key with this is the data is associated with the element specified with your selector so you can only retrieve the data by first referencing the element you want to retrieve the data from. At the same time, you can have 5 elements with 'svc-data' on them and you don't have to worry about conflicts. </p> <p><strong>Update2:</strong> Example in fiddle for you to see what I mean: <a href="http://jsfiddle.net/zYdRH/" rel="nofollow">http://jsfiddle.net/zYdRH/</a></p> <p><strong>Update3:</strong> One more example added is an instance variable. <a href="http://jsfiddle.net/zYdRH/3/" rel="nofollow">http://jsfiddle.net/zYdRH/3/</a></p> <p>Notice how everything is wrapped in an anonymous, self-executing function. This is a great approach to keep variables and functions out of the global scope. This means each item inside this function is only accessible to this function. I can't call any of these functions from another element, nor can I access the variables. Because of that, you can also store your JSON data in this scope to be accessed from any elements also in this scope. The newest example I added has a specific one called <strong>instanced</strong> but all the <strong>json vars</strong> can be used the same way - it was just more obvious for an example. But, note how <strong>instanced</strong> is a JSON object that is not stored to anything. It's global to the scope. Double click on a button to see each one showing it's associated word by the class. </p> <p>If your JSON data is unique to each button, the <em>data()</em> method is the way to go. However, if it's the same JSON for each but each element has it's own <em>piece</em> of the data it needs, the instance var is the way to go.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload