Error: Uncaught Typeerror: Undefined is not a Function

Asked

Viewed 20,624 times

1

<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
    <link rel="stylesheet" href="/resources/demos/style.css">
    <link href="~/Content/Menu.css" rel="stylesheet" />

    <script src="@Url.Content("~/Scripts/jquery-1.10.2.js")"></script>
    <script src="@Url.Content("~/Scripts/jquery-1.10.2.min.js")"></script>
    <script src="@Url.Content("~/Scripts/jquery-2.1.0.js")"></script>
    <script src="@Url.Content("~/Scripts/jquery-2.1.0.min.js")"></script>
    <script src="@Url.Content("~/Scripts/jquery-migrate-1.2.1.js")"></script>
    <script src="@Url.Content("~/Scripts/jquery-migrate-1.2.1.min.js")"></script>
    <script src="@Url.Content("~/Scripts/jquery-ui.js")"></script>
    <script src="@Url.Content("~/Scripts/jquery-ui.min.js")"></script>
    <script src="@Url.Content("~/Scripts/jquery.maskedinput.min.js")"></script>
    <script src="@Url.Content("~/Scripts/Menu.js")"></script>

    <script src="@Url.Content("~/Scripts/main.js")"></script>
    <script src="@Url.Content("~/Scripts/jquery.js")"></script>
    <script src="@Url.Content("~/Scripts/jquery-color.js")"></script>

    <script src="@Url.Content("~/Scripts/Util.js")"></script>
    <script src="@Url.Content("~/Scripts/Pesquisa/Pesquisa.js")"></script>

Here gives the error quoted above:

$("#txtCnpjPesquisa").mask("99.999.999/9999-99");

How do I resolve.

My jquery function

jQuery(function ($) {
    //$("#date").mask("99/99/9999");
    //$("#phone").mask("(99) 999-9999");
    //$("#cep").mask("99.999-99");
    //$("#cpf").mask("99.999.999-99");
    $("#txtCnpjPesquisa").mask("99.999.999/9999-99");
});

Following the suggestion of Filipe and Guilehermebernal, this became my cshtml:

<script src="@Url.Content("~/Scripts/jquery-ui.min.js")"></script>
    <script src="@Url.Content("~/Scripts/jquery.maskedinput.min.js")"></script>
    <script src="@Url.Content("~/Scripts/Menu.js")"></script>

    <script src="@Url.Content("~/Scripts/main.js")"></script>
    <script src="@Url.Content("~/Scripts/jquery.js")"></script>
    <script src="@Url.Content("~/Scripts/jquery-color.js")"></script>

    <script src="@Url.Content("~/Scripts/Util.js")"></script>
    <script src="@Url.Content("~/Scripts/Pesquisa/Pesquisa.js")"></script>
  • 1

    why the downvote?

  • 2

    Man, what you posted up there doesn’t help, that’s not your code, it’s just the includes. Have you checked if the element with the id "txtCnpjPesquisa" exists? Replace these includes all with your code.

  • @pnet, error on top where? Try to be more specific

  • The error is in this line of my jquery function: $("#txtCnpjPesquisa"). Mask("99.999.999/9999-99");

  • that code is where? inside which file?

  • What else do I have to do. When someone doesn’t understand, they vote negative just because they don’t understand the question?

  • @pnet, what is the file <script src="@Url.Content("~/Scripts/jquery.js")"></script> ? I think you’re including jQuery 5 times... I would take out all but version 2.1 min. And from there I was fixing the problem.

Show 2 more comments

4 answers

6


To pass the jQuery as an alias for $ ( and so avoid conflicts) must do so:

(function( $ ) {
  $(function() {
    // O seu código com dolar aqui      
  });
})(jQuery);

Apart from this detail it seems to me that you are including 5 versions of jQuery in the same document. That will (is to) give problems. Choose one and remove the others. If possible keep the last...

So your code could be:

(function( $ ) {
  $(function() {
    //$("#date").mask("99/99/9999");
    //$("#phone").mask("(99) 999-9999");
    //$("#cep").mask("99.999-99");
    //$("#cpf").mask("99.999.999-99");
    $("#txtCnpjPesquisa").mask("99.999.999/9999-99");
  });
})(jQuery);

Regarding versions of jQuery, 4 seems to me to be:

<script src="@Url.Content("~/Scripts/jquery-1.10.2.js")"></script>
<script src="@Url.Content("~/Scripts/jquery-1.10.2.min.js")"></script>
<script src="@Url.Content("~/Scripts/jquery-2.1.0.js")"></script>
<script src="@Url.Content("~/Scripts/jquery-2.1.0.min.js")"></script>

and possibly the last, lower:

<script src="@Url.Content("~/Scripts/jquery.js")"></script>

There must only be one.

  • 1

    I suggest leaving 1.10 because it is still compatible with IE version 8, which still has plenty of users.

  • Filipe, I have done as you have suggested, but keep giving me a hard time. I took everything, including the css link and even so still given error in the mask.

  • I think it was jquery.js. I removed it as you suggested and it seems to me.

  • @pnet great that the problem was solved, I did not talk about jquery.js because it could be your code and not the jquery library, it could be just coincidence of names.

  • @Sergio, your tip was Mio

1

Try it like this:

$(document).ready(function(){
     $("#txtCnpjPesquisa").mask("99.999.999/9999-99");
});

Another thing, you are making duplicate includes, the files with ".min.js" are cleaner versions, that is, without spaces and comments, the load of these files is faster when you open a page.

Remove the following lines (includes duplicated):

<script src="@Url.Content("~/Scripts/jquery-1.10.2.js")"></script>
<script src="@Url.Content("~/Scripts/jquery-2.1.0.js")"></script>
<script src="@Url.Content("~/Scripts/jquery-migrate-1.2.1.js")"></script>
<script src="@Url.Content("~/Scripts/jquery-ui.js")"></script>

You are also including two versions of jquery:

<script src="@Url.Content("~/Scripts/jquery-1.10.2.js")"></script>
<script src="@Url.Content("~/Scripts/jquery-1.10.2.min.js")"></script>
<script src="@Url.Content("~/Scripts/jquery-2.1.0.js")"></script>
<script src="@Url.Content("~/Scripts/jquery-2.1.0.min.js")"></script>

Delete a version, I suggest you keep version 1.10 because it is compatible with IE8, delete the lines:

<script src="@Url.Content("~/Scripts/jquery-2.1.0.js")"></script>
<script src="@Url.Content("~/Scripts/jquery-2.1.0.min.js")"></script>
  • How so includes duplicates. I really understood the issue of the versions, but the maskedinput if I withdraw can no longer mask fields

  • jquery-1.10.2.js, jquery-1.10.2.min.js, jquery-2.1.0.js, jquery-2.1.0.min.js, jquery.js are FIVE copies of jquery being included. Use only one. And if you used the minified file you don’t need to use the minified.

  • @pnet do not remove the maskedinput, was included and did not realize, I apologize.

  • @Guilhermebernal yes, include that in the answer.

  • I did as requested and still gives the same error. And also does not recognize this link:<link rel="stylesheet" href="/Resources/demos/style.css">

  • Excuse me, but what is minified file

  • @pnet As the two answers say, $ is not defined in the function argument. Use jQuery(function() {...}); without the argument or some variant of it. Minified file is equal to non-minified file, but written using fewer bytes to compress. Both have the same functionality.

  • @pnet minificados are compressed files, that is, free of comments, line breaks and depending on the type of minificador that is used it can even change the nomenclature of its variables to be able to decrease to the maximum the size of its file. Minified files are uploaded faster that consequently improve site performance.

Show 3 more comments

0

was with a similar problem and solved with the following code

(function( $ ) {
  $(function() {
    // O seu código com dolar aqui      
  });
})(jQuery);

-2

In my case, this error happened because I was loading the.js file that called . Mask before jquery. After fixing the order that the files were loaded the error no longer occurred.

Browser other questions tagged

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