0

I have a js file with Jquery function now i have to call that function on document ready and inside another PHP file i tried below codes it worked for PHP file but for the JS file it is saying $.test is not a function

jq.js file for this error message was $.test is not a function.

$(document).ready(function(){

    $.test();

    $.test =    function(){
        alert('HELLO WORLD');
    }
});

add.php file For this worked.

.....SOME PHP CODES HERE ....
<td class="del"><span class="fa fa-times"></span></td>

<script>
    $('.delI').unbind().click(function(){       
        $.test();       
    });
</script>

I have to call this test() function inside the same JS file and in another PHP file too please give me a solution.. thanks.

2
  • You have to call it after you define it, not before Commented Jul 21, 2016 at 10:22
  • @A.Wolff it worked Thanks. do you think this method secure? or there any better way to do this? Commented Jul 21, 2016 at 10:25

2 Answers 2

3

The problem is that you call $.test before you defined it. Call it after the definition:

$(document).ready(function(){

    $.test =    function(){
        alert('HELLO WORLD');
    }

    $.test();
});
Sign up to request clarification or add additional context in comments.

Comments

1

Simply do this:

$(document).ready(function(){

    $.test =    function(){
        alert('HELLO WORLD');
    }
})();

Call the function after you define it, not before.

Or:

$(document).ready(function(){

            $.test =    function(){
                alert('HELLO WORLD');
            }
      $.test();
        });

Comments

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.