0

For example:

<input id="#me" type="button" callme="doAction()"/>
    
<script>
     var result = callFunction( $("#me").attr('callme') );       /// ?????????
</script>
2
  • What do your callFunction does? Are you expecting to pass a string? Commented Oct 11, 2010 at 13:05
  • 1
    What exactly are you trying to accomplish? Commented Oct 11, 2010 at 13:13

4 Answers 4

2

Try this:

<script>
    var callMe = $("#me").attr("callme");
    var result = eval(callMe);
</script>
Sign up to request clarification or add additional context in comments.

Comments

1

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.

3 Comments

I get this error: (f||jQuery.noop)(); f is not a function ?? the function exists...
jQuery.noop() is in jQuery version 1.4+
jQuery.noop can be replaced with function(){} if you need to or you could assign it yourself somewhere with jQuery.noop = function(){};
1

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().

2 Comments

I like this but, I get this error: (f)(); f is not a function ?? the function exists...
Oops, sorry. Try this (window[$("#me").attr('callme')] || function() {})();
0

I think maybe this is the solution:

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

But how to get the returned value??

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.