Rahul Chaudhari
Rahul Chaudhari

Reputation: 148

How to call Jquery Plugin function from outside of plugin

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.

Upvotes: 0

Views: 80

Answers (1)

Manprit Singh Sahota
Manprit Singh Sahota

Reputation: 1339

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>

Upvotes: 1

Related Questions