Friday, January 2, 2009

[jQuery] Re: How can I call a function if I have that function name stored in a string?

I don't know about the $.fn.myplugin part - that's not how you would
typically call a plugin function in the first place.

But in general, given any object 'foo' and any property 'bar', you can
reference the 'bar' property in either of two ways:

foo.bar

Or:

foo['bar']

If 'bar' is a method, you can call it with:

foo.bar(...);

Or:

foo['bar'](...);

Either one means exactly the same thing.

One common use for this is with show and hide methods. Instead of coding:

if( doShow )
$('#foo').show();
else
$('#foo').hide();

You can code:

$('#foo')[ doShow ? 'show' : 'hide' ]();

-Mike

> From: yellow1912
>
> Something like this
> var func = 'myFunc';
>
> Can I call the function like this for example:
>
> $.fn.myplugin.(func)();
>
> (I'm using this in a plugin I'm working on)
>
> Thank you very much
>


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "jQuery (English)" group.
To post to this group, send email to jquery-en@googlegroups.com
To unsubscribe from this group, send email to jquery-en+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/jquery-en?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments:

Post a Comment