Home javascript jQuery conflict: $ is not a function

jQuery conflict: $ is not a function

Author

Date

Category

The console gives this error:

Uncaught TypeError: $ is not a function
  at profile.php? id = 1: 122

I tried it in an empty .php file, everything works fine there, I concluded that some internal script interferes with the work. $ .noConflict (); didn’t help. How can I solve this problem?

PHP file:

& lt;! DOCTYPE html & gt;
& lt; html lang = "en" & gt;
& lt; head & gt;
  & lt; script type = "text / javascript" src = "https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" & gt; & lt; / script & gt;
  & lt; script type = "text / javascript" src = "/ engine / smiles / emojionearea.js" & gt; & lt; / script & gt;
  & lt;! - [if lt IE 9] & gt;
  & lt; script src = "https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js" & gt; & lt; / script & gt;
  & lt; script src = "https://oss.maxcdn.com/respond/1.4.2/respond.min.js" & gt; & lt; / script & gt;
  & lt;! [endif] - & gt;
& lt; / head & gt;

Styles and other non-script entries omitted in & lt; head & gt; .

All plugins respond with Response code: 200 .

The script itself:

& lt; div class = "box-v5 panel" & gt;
  & lt; script type = "text / javascript" & gt;
    $ .noConflict ();
    $ (document) .ready (function () {// The same line that the console swears at
      $ ("# emojionearea1"). emojioneArea ({
        pickerPosition: "left",
        tonesStyle: "bullet"
      });
    });
  & lt; / script & gt;
  & lt; div class = "panel-heading padding-0 bg-white border-none" & gt;
  & lt; textarea id = "emojionearea1" & gt; Default: smile: & lt; / textarea & gt;

Answer 1, authority 100%

To avoid conflicts with $ , you can use this option:

jQuery (document) .ready (function ($) {
  // use jQuery like $
}

The jQuery object is passed to the function called upon document.ready as the first parameter. Since the parameter can be given any name, $ is fine for this. And since this is a local variable (parameter), when using $ inside a function, it will be taken, and not the global variable $ (which somewhere in the code was replaced with jQuery to something else).


Likewise for the “short” version of document.ready :

jQuery (function ($) {
  // use jQuery like $
});

When used outside of document.ready :

(function ($) {
  // use jQuery like $
}) (jQuery);

Answer 2, authority 100%

jQuery (document) .ready (function ($) {
});

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions