user2955394
user2955394

Reputation: 1183

Why i can not use "$" for call JQuery function in MVC

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 "$"

Upvotes: 1

Views: 83

Answers (4)

Ragini Gurram
Ragini Gurram

Reputation: 51

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

Upvotes: 0

Ragini Gurram
Ragini Gurram

Reputation: 51

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

Upvotes: 0

Ethan Hunt
Ethan Hunt

Reputation: 141

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

$ = jQuery.noConflict();

Upvotes: 0

Wesley Smith
Wesley Smith

Reputation: 19571

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
});

Upvotes: 1

Related Questions