Pavel Staselun
Pavel Staselun

Reputation: 2010

jQuery data() from a function

I set jQuery object's data using a function:

oTabSearch.data('search_data', collect_data());

Is there a way to somehow recalculate this data piece e.g. if I get data from object:

var search_data = oTabSearch.data('search_data');

it would first set 'search_data' data using collect_data() function and then return it?

Upvotes: 0

Views: 82

Answers (1)

Reinstate Monica Cellio
Reinstate Monica Cellio

Reputation: 26143

You can do this...

oTabSearch.data('search_data', collect_data);  // notice no parenthesis

and to run the function...

var search_data = oTabSearch.data('search_data')();  // notice the empty parenthesis

That effectively stores a copy of the function as a data object and then runs it again when you request. It does not store any return value.

Here's an example jsFiddle...

http://jsfiddle.net/Tr3zK/

Click "mydiv" to see the data value, therefore calling the function again.

Upvotes: 2

Related Questions