Help with jquery Uncaught Typeerror: $ is not a Function

Asked

Viewed 13,141 times

1

I have a problem with a filter selector, when I choose a city to filter information about it, the browser console shows me the error below. I can’t tell which version of Jquery is being used on the site, I am parachuting into the world of js rs.

In localhost with jquery 2 worked perfectly, after I climbed to the server, started giving this error and stopped working the filter.

Jquery used on the site: https://jsfiddle.net/gLep8kdn/

Full code of custom js https://jsfiddle.net/0kLjj0y5/

Uncaught Typeerror: $ is not a Function at Htmlselectelement.filterlist (custom.js:3)

Code

$('select[name="list_citys"]').change(filterList);   3 ª linha

function filterList(e) {
var value = e.target.value;
$(".list_citys_shop .new-citys").parent().parent().removeClass("hidden");
if (value == "all_city") $(".list_citys_shop .new-citys").slideDown();
else {
    $(".list_citys_shop .new-citys").slideUp();

    $(".list_citys_shop ul:not([data-category*=" + value + "])").parent().addClass("hidden");
    $(".list_citys_shop ul[data-category*=" + value + "]").find(".new-citys").stop().slideDown();
  }                                                                           
}
  • 2

    Probably the jQuery was not loaded.

  • It is being loaded normally as the other functions of the site work.

  • Can you put all the JS code? What’s in the two lines before the line that gives the error?

  • Only comments on what the code performs. After this code comes the bootstrap js https://maxcdn.bootstrapcdn.com/bootstrap.3.7/js/bootstrap.js

  • Here the complete code https://jsfiddle.net/0kLjj0y5/

  • 1

    The code in jsfiddle gives the same error, for the same (probable) reason why it gives error on your site. Check the point at which indicates the path to jQuery.

  • And where is importing jQuery?

  • No @Andersoncarloswoss jquery is being called in another file before custom js <script src="/Assets/jquery.min.js"></script> <script src=/Assets/custom.js"></script> .

  • The code in production http://loja.bluebeach.com.br/preview/testelojas2-126/v Ctrl + u and search by jquery and custom.js

  • Leandro mix Angular and jQuery can be one of the reasons, really do not see need to use both, despite different and their way of working, so I looked in the code of the page something is overwriting the global variable $.

  • Well, William, the worst thing is that I have no way to edit or remove files, because the site was made on those platforms ready and the guys barely let you touch the js, they analyze all the code before publishing.

  • I tried to play the minimum of your code in Jsfiddle (https://jsfiddle.net/ahp9cyn3/) and it seems to be working.

  • 2

    @Andersoncarloswoss is a script that overwrites the $, maybe the Angular.js

  • @Andersoncarloswosso custom.js is called a 10 lines below the jquery, as you said, it is working normally in jsfiddle, it can be an angular file overwriting the $ even, I will make a test equal the William suggested.

  • Guys, I’ll test William’s tip, but I’ll probably only get response from the support (Change the custom js) there by Monday afternoon, then I post the result here. Thank you so much for your help.

Show 10 more comments

2 answers

6


It’s probably the order that added jQuery, the order in HTML should always be this:

<script src="caminho do jquery/jquery.js"></script>
<script src="caminho do jquery/plugin-jquery1.js"></script>
<script src="caminho do jquery/plugin-jquery2.js"></script>
<script src="caminho do jquery/plugin-jquery3.js"></script>
<script src="/assets/custom.js"></script>

If you are using bootstrap, then the order will be this:

<script src="caminho do jquery/jquery.js"></script>
<script src="caminho do jquery/bootstrap.js"></script>
<script src="caminho do jquery/plugin-jquery1.js"></script>
<script src="caminho do jquery/plugin-jquery2.js"></script>
<script src="caminho do jquery/plugin-jquery3.js"></script>
<script src="/assets/custom.js"></script>

If you do something like this:

<script src="caminho do jquery/bootstrap.js"></script>
<script src="caminho do jquery/jquery.js"></script>
<script src="/assets/custom.js"></script>

Or so:

<script src="/assets/custom.js"></script>
<script src="caminho do jquery/jquery.js"></script>

It will fail because bootstrap uses jQuery but in the order of loading jQuery has not yet been requested.

So remember, jQuery must come first of all

Note that if you are using the attribute async may also cause failures, such as:

<script async src="caminho do jquery/jquery.js"></script>
<script src="/assets/custom.js"></script>

Because custom.js won’t wait for jquery.js to load, another important thing to note is if you’re not loading two jQuery at the same time, for example:

<script src="caminho do jquery/jquery.js"></script>
<script src="caminho do jquery/jquery.min.js"></script>

Protecting the scope

Another thing that may be occurring is that some library like Angular.js (or another) may be overwriting the variable $:

  • One way to solve it is to change the $ for jQuery

  • Another way is to isolate the scope, so for example:

    (function ($) {
        $(...).funcao(...);
    })(jQuery);
    

note: I really don’t see the need to mix angular.js with jQuery, even if both are distinct, Angular is very specific, if you’re going to use for a simple thing like a button just then better not use Angular.

  • I believe the order is correct, William, code in production > http://loja.bluebeach.com.br/preview/testelojas2-126/v

  • @Leonardo I updated the answer, from a look at the end I added, this should help correct

  • I will test both ways, but I will probably only get response from the support (Change the file) there Monday afternoon, then I comment here if it worked. Thanks for the help.

  • @Leonardo goes for me, the likely error is mixing angular.js with jquery :/, but you can expect a return of support if you want.

  • I don’t know much about jquery and angular, but I think it’s more likely. In fact I depend on the support, only they can update the custom file,js, that’s tensors, again thank you very much, as soon as I receive a reply from them, I notice if it worked.

  • 1

    I received a response from support, your suggestion to exchange all the $ for jQuery worked perfectly. Thank you @Guilherme.

Show 1 more comment

2

If you are working with Wordpress, as in my case, use this solution:

;(function($){
    // seu código
})(jQuery);

Caught in SOF

Browser other questions tagged

You are not signed in. Login or sign up in order to post.