emphaticsunshine
emphaticsunshine

Reputation: 3765

Jquery call function dynamically

I have a following scenario that I have to call a function on a jquery object dynamically. The code looks like this :

$('div[class]').each(function(){
    var class=$(this).attr('class');

    // I want to now check if that function with the name of class exists for jquery
    // object. I need help here.
    // if that function exists, i need to apply that function to the enumerated object.
});

I want to now check if that function with the name of class exists for jQuery object. I need help here. If that function exists, I need to apply that function to the enumerated object.

Upvotes: 0

Views: 782

Answers (1)

alh84001
alh84001

Reputation: 1283

I'm writing this of the top of my head so it might not work, but try:

if(jQuery.isFunction(jQuery[class]))
     jQuery[class](this);

Edit: if the function is a jquery method then try with:

 if(jQuery.isFunction(jQuery.fn[class])) {
     jQuery.fn[class](this);
     jQuery(this)[class]();    // alternative call syntax
 }

Upvotes: 2

Related Questions