PandaWood
PandaWood

Reputation: 8343

Simplest jQuery plugin, why doesn't this work?

I must be missing something very basic & think I need a cardboard partner. I've added a new method to jQuery, yet, as in the firebug command-line experience below, it just doesn't work because the method doesn't exist.

The jQuery tutorials all say to write exactly this code, what is wrong with it?

$.fn.bob = function() {alert('bob!');}

>function()

$.bob()

>TypeError: $.bob is not a function

Upvotes: 0

Views: 67

Answers (2)

Frederik Wordenskjold
Frederik Wordenskjold

Reputation: 10221

$.fn refers to the jQuery function $():

So your function would have to be called like this:

$('anything').bob();

You can also extend the jQuery object directly, and call the function as you intended:

$.bob = function(){ alert('bob!') }
$.bob();

Upvotes: 3

kennypu
kennypu

Reputation: 6051

you need to use it on a jquery object:

$('body').bob()

Upvotes: 1

Related Questions