mbejda
mbejda

Reputation: 1471

Jquery alert hello Plugin doesnt work

Im trying to make simple Jquery plugin. I went through the Jquery documention and I reduced the layout to make a simple alert. This is my Jquery Plugin code.

        (function($){
      $.fn.foo = function(){
        alert("HI");
      };


})(jQuery);

Than on my main page I have the Jquery reference URL and this code

  <script type="text/javascript"src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
  foo();
    });
</script>

I keep getting errors in the debugger SCRIPT5009: 'foo' is undefined

Please help me fix this.

Upvotes: 0

Views: 206

Answers (2)

Mark Kramer
Mark Kramer

Reputation: 3214

You made a jquery plugin, you have to reference it in seperate jQuery function to fire it.

Here is my working example: http://jsfiddle.net/MarkKramer/Z9vz2/

Upvotes: 0

Jayendra
Jayendra

Reputation: 52769

you can invoke the foo method on some element or need to define it differently e.g.

<div id="test" />

Plugin -

(function($){
      $.fn.foo = function(){
        alert("HI");
      };

       $.otherfoo = function(){
            alert('Hiiiii');
        };
})(jQuery);

Test -

$(document).ready(function(){
  $('#test').foo();
  $.otherfoo();  
});

should work fine.

Upvotes: 2

Related Questions