0

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.

4
  • I can't clearly see your use case here. What are you trying to accomplish? Explain it as in if I do this, then this should happen! If this element has the class abc I want the function abc to be executed, etc... Commented Oct 1, 2011 at 16:47
  • Yes, if the element has class abc then I want abc to be executed on that element if that function exists. Commented Oct 1, 2011 at 16:50
  • 1
    Exists where, in the global namespace, in jQuery's jQuery.fn.function_here? Also, are you sure your elements will have only one class? Commented Oct 1, 2011 at 16:53
  • Multiple classes is a different case. I will parse it if I have that scenario. I want to check if it exists in globalspace. I have function like this: $.fn.funtion_name() Commented Oct 1, 2011 at 17:00

1 Answer 1

2

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
 }
Sign up to request clarification or add additional context in comments.

1 Comment

I think that is pretty close that I was looking for. Thank you

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.