1

When i use JQuery like this it's not work

$(document).ready(function() {
    alert("abc");
});

But When i use JQuery like this it's work

JQuery(document).ready(function() {
    alert("abc");
});

How to config it's for use with "$"

1
  • 3
    there may be another library overriding jQuery or asp.net may be calling jQuery.noConflict()... Commented Jan 21, 2015 at 3:35

4 Answers 4

1

Your problem has to do with scoping. Basically, some other plugin or library you are using is already using $ to alias something other than jQuery.

To use jQuery as you are used to, like $('#myInput').val(); you will need to place your code inside an IIFE - Immediately Invoked Function Expression. Inside that function, $ will be locally scoped to refer to jQuery and will work as you expect

  // IIFE - Immediately Invoked Function Expression
  (function($, window, document) {
      // The $ is now locally scoped

      // use $ normally inside here

  }(window.jQuery, window, document));

I have also read that you can use the below, but I have not tested it:

jQuery(function($){
// The $ is now locally scoped
// use $ normally
});
Sign up to request clarification or add additional context in comments.

Comments

0

I also got the error once..I used this to resolve, may be due to different jQuery version.

$ = jQuery.noConflict();

Comments

0

please check in your code if some where you are using jQuery.noConflict(); The below link will give you more insight what it actually mean when it should be used jquery.noconflict

Comments

0

try and use $j= jQuery.noConflict(); i think in you code some other java library is using the $ notation . once you declared it you can change the code as

$(document).ready(function() {
    alert("abc");
});

and this should solve your problem

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.