Jquery - how to set and retrieve values ​​from an array stored using data ()

I am trying to create an array, assign it to a div element, press () a value and get it again at a later stage.

Can someone tell me what I am missing ...:

// set Array // $(this) is the HTML-element which should get array assigned var stack = [], stack.push('#'+$(this).find(':jqmData(role="page")').attr('id')), $(this).data( stack ); 

Using

 console.log( $(this).data(stack) ); 

I am returning the entire HTML element as well

 console.log( $(this).data(stack[0] OR stack.length) ); 

does not work.

How can I access the elements that I (presumably ...) are placed in an array?

+4
source share
3 answers

You need to provide your details. For instance:

 $("div").data("stack", []); //This sets the data key "stack" to an empty array $("div").data("stack").push(123); //This retrieves the array and pushes a number to the array console.log($("div").data("stack")); //This prints the array 
+3
source

To assign data to an element, you must give it the key for the index.

Put

 $(this).data('stack', stack); 

Get

 var stack = $(this).data('stack'); 
+1
source

Data takes a pair of keys, values. You do not pass the key.

 $(this).data('stack', stack ); 

Then, to get it:

 $(this).data('stack'); 
0
source

All Articles