0

I am using asp.net mvc application. From last 2 days i am trying and following some Internet solutions to call the MyPlugin function from outside the JS . Please help me to find what wrong i am doung.

I had added myplugin JS and written some lines of code and now i am trying to access that function from View script.

My View :

<head>
  <script src="~/Scripts/jquery-3.3.1.min.js"></script>
  <script src="~/Scripts/MyPlugin.js"></script>
</head>
<body>
  <div id="divMain" >
  </div>
</body>

<script>
 $(document).ready(function () {

    $("#divMain").MyPlugin();

 });
</script>

MyPlugin JS:

(function ($) {
    $.fn.MyPlugin= function () {
        alert("ready to start!!!!");
    };

})(jQuery);

And This message i got :

"Uncaught TypeError: $(...).MyPlugin is not a function"

Please help me to find a solution of this problem. I want to call that Plugin's function.

3
  • I can see that you have not added jQuery on your page.. Please add it before your plugin js and try. Commented Jul 18, 2019 at 7:02
  • The code you've shown works absolutely fine: jsfiddle.net/ke6sy8ux. Have you debugged this at all? Are there any other errors in the console? Commented Jul 18, 2019 at 7:32
  • OK Thanks @RoryMcCrossan , let me check it again. Commented Jul 18, 2019 at 8:43

1 Answer 1

1

You have to add jquery prior to your plugin js.

<head>
  <script src="~/Scripts/jquery.js"></script>
  <script src="~/Scripts/MyPlugin.js"></script>
</head>
<body>
  <div id="divMain" >
  </div>
</body>

<script>
 $(document).ready(function () {

    $("#divMain").MyPlugin();

 });
</script>
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks @Manprit , I have just tried and getting same error. I have updated my question again.
The code works fine for me. May be clearing cache can help you.
This is too odd. i have tried with new project and its not working for me. code and everything is ok but not getting where is the actual problem
Yes . I got error message jquery-3.3.1.min.js:2 Uncaught TypeError: $(...).MyPlugin is not a function at HTMLDocument.<anonymous> (Example:57) at l (jquery-3.3.1.min.js:2) at c (jquery-3.3.1.min.js:2)
Yes..!!!! Solved. The main reason was my Layout page. The Jquery bundle was execute after my JS Plugin so i faced this problem.
|

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.