Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if function exist?

How to check if is function on jquery, but function is in another .js file?

validation.js:

if ($.isFunction('payment')) {
    $('[data-numeric]').payment('restrictNumeric');
    $('.cc-number').payment('formatCardNumber');
    $('.cc-exp').payment('formatCardExpiry');
    $('.cc-cvc').payment('formatCardCVC');
}

this is false because func payments is in the payments.js .

like image 709
Haroldas Avatar asked May 13 '15 13:05

Haroldas


3 Answers

Try like this

if (typeof payment === "function")
{
  // Do something
}
like image 68
Anik Islam Abhi Avatar answered Oct 04 '22 00:10

Anik Islam Abhi


problem is solved. its works:

if ($.fn.payment) {
    //do something
}
like image 30
Haroldas Avatar answered Oct 03 '22 23:10

Haroldas


Try to check like as follows,

 if (typeof payment !== 'undefined' && $.isFunction(payment)) {
    $('[data-numeric]').payment('restrictNumeric');
    $('.cc-number').payment('formatCardNumber');
    $('.cc-exp').payment('formatCardExpiry');
    $('.cc-cvc').payment('formatCardCVC');
 }
like image 44
Shijin TR Avatar answered Oct 04 '22 01:10

Shijin TR