How do you detect if a function is a method for a jQuery element?
For example, $.fn.fadeInthis is a function:
typeof $.fn.fadeIn === 'function' //true
However, I need a way to distinguish it from the regular non-jQuery method. The goal is to be able to pass the function as a parameter, and then call the function correctly.
The following is an example using a function with a name doItthat takes a jQuery element and a function applied to the element.
HTML example:
<h1>jQuery Function Test</h1>
<p class=t1>Highlight this line.</p>
<p class=t2>Hide this line.</p>
JavaScript example:
function doIt(elem, func) {
if (func === 'a jQuery function')
func.apply(elem);
else
func(elem);
}
function highlight(elem) {
elem.css('background-color', 'gold');
}
doIt($('p.t1'), highlight);
doIt($('p.t2'), $.fn.fadeOut);
Line # 2 needs JavaScript help.
Interactive version:
http://jsfiddle.net/sxduwke9/
source
share