28

The jQuery .data() documentation says the following:

The .data() method allows us to attach data of any type to DOM element

I assume "any type" refers to functions as well. Say I have a div with the id foo as such:

<div id="foo">Foo!</div>

And I'd like to store a function in it called say that takes a parameter. According to the documentation I'd store the function this way:

$("#foo").data("say", function(message){
   alert("Foo says "+message);
});

My question is, how do I invoke the function with a parameter when I wish to do so.

$("#foo").data("say"); should return the function, but how would I pass a parameter to it?

3 Answers 3

43

A better alternative to storing functions in data is using custom events. This can be done easily with on and trigger:

$('#foo').on('say', function(e, arg){
    alert('Foo says ' + arg);
});

$('#foo').trigger('say', 'hello');

Example: http://jsfiddle.net/tW66j/

Note: on earlier versions of jQuery (until 1.7), .bind was used instead of .on.

Sign up to request clarification or add additional context in comments.

5 Comments

+1 - this is great advice, but Darin answered my question exactly. Thank you very much.
sometimes it's useful. consider a case where you want an uniform way to fetch data that is semantically associated with the DOM node, but the implementation of the data-fetching varies depending on node type.
We could, of course, store an XPath or the target DOM node object itself to fetch but a callback function is more extensible and requires no duplication of DOM node objects
For anybody coming back to this several years later, 'bind' has been depreciated in favour of 'on'. $('#foo').on('say', function(e, arg){alert('Foo says ' + arg);});
@ATfPT - Good point, I updated the answer and the example. Thanks! By the way, the documentation doesn't say bind is deprecated (yet?). This is what deprecation looks like: die
28
var myFunction = $("#foo").data("say");
myFunction(someParameter);

Comments

4

Here's a more elegant way to call the method.

// store the method with data
$('#foo').data('bar',function(){console.log("Givin it the business!")});

// Simple execution (this will not keep the method in scope)
$('#foo').data('bar')();

// scoped method execution with call([scope,arguments[]]), takes arguments with comma separation
$('#foo').data('bar').call($('#foo').first()[0],'arguemnts','for','method');

// scoped method execution with apply(scope[,argument[]]), takes an array of arguments
$('#foo').data('bar').apply($('#foo').first()[0],['arguemnts','for','method']);

Apply Method: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/apply

Call Method: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/call

3 Comments

Upvote for storing and calling functions using .data. However, I tried this and when you call a function stored this way $('#foo').data('bar')(); it looses its context. $(this) now refers to the DOMWindow, not $(#foo). Kobi's solution however keeps the function's context for $(this).
yeah, that's not how to keep it in scope, you'll need to use the call or apply method to keep the execution in scope.
which jquery version is required for data() attribute

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.