4

I'm trying to create an array, assign it to a div-element, push() a value and retrieve it again at a later stage.

Can someone tell me, what I'm 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'm getting back the whole HTML element, while

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

don't work.

How can I access the elements that I (presumably...) pushed into the array?

3 Answers 3

3

You need to name your data. For example:

$("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
Sign up to request clarification or add additional context in comments.

Comments

1

To assign data to an element you have to give it a key to index by.

PUT

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

GET

var stack = $(this).data('stack');

Comments

0

Data takes a key, value pair. You're not passing in the key.

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

Then to retrieve it:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.