views:

53

answers:

4

For example:

<input id="#me" type="button" callme="doAction()"/>

<script>
 var result = callFunction( $("#me").attr('callme') );       /// ?????????
</script>
A: 

I think maybe this is the solution:

<script>
setTimeout( $("#me").attr('callme'), 0 );
</script>

But how to get the returned value??

Cris Hong Kong CRISHK
+1  A: 

Try this:

<script>
    var callMe = $("#me").attr("callme");
    var result = eval(callMe);
</script>
Tim S. Van Haren
hmmmmmmmmmmmmmmmmmmmmm
Cris Hong Kong CRISHK
+1  A: 

if the attribute callme is the name (identifier) of a function just use

<input id="#me" type="button" callme="doAction"/>



(window[$("#me").attr('callme')] || function() {})();

so you can avoid using an expensive eval()

Fabrizio Calderan
I like this but, I get this error: (f)(); f is not a function ?? the function exists...
Cris Hong Kong CRISHK
Oops, sorry. Try this(window[$("#me").attr('callme')] || function() {})();
Fabrizio Calderan
+1  A: 

A better solution would be to not include the function in your HTML markup. It is almost always better to separate your markup from your scripting.

Instead consider adding it to your dom object using

$('#me').data('callme', function(){...});

or

$('#me').data('callme', doAction);

and calling it using

$('#me').data('callme')()

or a little safer

($('#me').data('callme')||jQuery.noop)()

See the jQuery documentation for more details on the data function.

tKe
I get this error: (f||jQuery.noop)(); f is not a function ?? the function exists...
Cris Hong Kong CRISHK
`jQuery.noop()` is in jQuery version 1.4+
fudgey
`jQuery.noop` can be replaced with `function(){}` if you need toor you could assign it yourself somewhere with `jQuery.noop = function(){};`
tKe