Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery .fn is saying "not a function"

When I call this custom function

$.fn.inputBoxHelper = function () {
    var args = arguments[0] || {};
    var matchingElem = $.grep(this, function (e) { return $(e).val() == $(e).attr('title'); });
    $(matchingElem).addClass(args.className);

    this.bind({
        focus: function(){
            if ($(this).val().trim() == $(this).attr('title')) { $(this).val(emptyString).removeClass('gray'); }
        },
        blur: function(){
            if ($(this).val().trim() == emptyString) { $(this).val($(this).attr('title')).addClass('gray'); }
        }
    });
    return this;
}

Like this

$('input:text').inputBoxHelper({ className: 'gray' });

It gives me the error when I call it

$("input:text,input:checkbox").inputBoxHelper is not a function
file:///D:/repository/scripts/script_tests/test.js
Line 20

Even when I change the function to just

$.fn.inputBoxHelper = function () {return this;}

it does the same thing. It seems to run fine and work correctly as all the functionality works on the input boxes but get the error. Anyone have any ideas?

like image 803
Micah Avatar asked Nov 17 '10 17:11

Micah


People also ask

How to solve the $(...) on if not a function jQuery error?

To solve the "$ (...).on if not a function" jQuery error, make sure to load a recent version of the jQuery library and check if you are including any scripts that are overriding the value for the global dollar sign $ variable after you load the jQuery script. Use the search field on my Home Page to filter through my more than 1,000 articles.

What is the use of jQuery FN extend?

Description: Merge the contents of an object onto the jQuery prototype to provide new jQuery instance methods. An object to merge onto the jQuery prototype. The jQuery.fn.extend () method extends the jQuery prototype ( $.fn) object to provide new methods that can be chained to the jQuery () function.

What does $$ is not a function mean in WordPress?

$ is not a function WordPress error occurs when the code comes before the jQuery library. For example, if a plugin or theme calls a code before calling the right library, you get this error. By default, WordPress doesn’t understand $ as jQuery and you have to make some modifications to fix this error.

Why is $is not a function not defined in Firefox?

If you tested your page using FireBug in Firefox and found the error "$ is not a function" or "$ is not defined", most likely you are using other libraries in your page. Libraries that conflicts with jQuery includes MooTools, Prototype, etc.


2 Answers

Check that you're not loading jQuery into the page twice. Some plugins or plugin libraries like to include jQuery. So it can overwrite your existing jQuery instance and override the $ and jQuery variables with a new – fresh – instance causing existing plugins to be undefined. Plugins are added to the prototype of jQuery when they are defined. So if you destroy the only instance of that object, then the prototype is reset to its original form. And therefore those plugins are no longer part of that prototype.

Also, it may be nothing, JSLint is reporting that you're missing a semicolon on the last line of that function definition you posted. It ought to be:

$.fn.inputBoxHelper = function () {
  var args = arguments[0] || {};
  var matchingElem = $.grep(this, function (e) { return $(e).val() == $(e).attr('title'); });
  $(matchingElem).addClass(args.className);

  this.bind({
     focus: function(){
          if ($(this).val().trim() == $(this).attr('title')) { $(this).val(emptyString).removeClass('gray'); }
      },
      blur: function(){
          if ($(this).val().trim() == emptyString) { $(this).val($(this).attr('title')).addClass('gray'); }
      }
  });
  return this;
};

It's always a good idea to run all of your javascript through JSlint. It can help you find stuff that you might spend hours looking for otherwise.

like image 83
sholsinger Avatar answered Sep 30 '22 01:09

sholsinger


The plugin needs to be loaded before it can be used.

Comments above confirm that OP was attempting to use the plugin before it was loaded.

like image 20
user113716 Avatar answered Sep 30 '22 01:09

user113716