jQuery’s$ doesn’t work

Asked

Viewed 614 times

2

I’m using a code that uses $ jQuery, only when using the cipher it error in the console and the jQuery code ends up not working. The only solution was to use jQuery instead of $ :

jQuery(document).ready(function(){

    jQuery("#finalizarcadastrojovem").click(function(){

        var form = jQuery("#nome").val();

        console.log(form);

        return false;

    })
})

I wish I knew how to fix it 'cause I want to use it $ and not jQuery

  • 7

    Are you using another framework that uses $ as well? Sometimes because of order and compatibility, the $ ends up being overwritten, for this has the function $.noConflict.

3 answers

5

Wakim is right, there is probably another code (probably another framework) using $. But what you may not know is that jQuery passes the object itself jQuery as an argument from callback of ready. So you can do it like this:

jQuery(document).ready(function($){    
    $("#finalizarcadastrojovem").click(function(){
        var form = $("#nome").val();   
        console.log(form);    
        return false;   
    });
});
  • Still, it’s worth checking the other frameworks you use and using $.noConflict() to avoid the risk of disrupting the other framework.

  • Actually I’m pretty sure jQuery also defines the object jQuery globally and the $ as shortcut. How it should load another framework that overwrites the $, soon only leaves the jQuery.

  • Exactly, @Wakim. And use noConflit to define $ would interfere with the other framework. So I think the solution I am proposing is reasonable.

  • @bfavaretto, the noConflict does not define the $, take a look: "If you need to use Another Javascript library alongside jQuery, Return control of $ back to the other library with a call to $.noConflict()" http://api.jquery.com/jquery.noconflict/

  • 2

    @Pedrolorentz This is if jQuery is loaded later. In the question code, I believe it is being loaded before the other framework.

5


You must have some other script forcing jQuery instead of $

search for jQuery.noConflict()

but to help you try:

<script type="text/javascript">
    var $ = jQuery.noConflict();
    $(document).ready(function(){

        $("#finalizarcadastrojovem").click(function(){

            var form = jQuery("#nome").val();

            console.log(form);

            return false;

        })
    });
</script>

But the advice is that you search the scripts and try to find this forced jQuery, if you have nothing I do not know what to say, I use enough jQuery.noConflict() from a look http://api.jquery.com/jquery.noconflict/

  • var $ = jQuery.noConflict(); will erase the reference to $ if another library is already using it and forcing jQuery (!). The probability of generating a new error is high.

1

There are several ways to restore the dollar.

Some CMS like wordpress use by default jQuery instead of $ to avoid possible conflicts with other libraries. Keep in mind to use jQuery is safer than $.

To force globally can do:

window.$ = jQuery;

Even that in this way removes the $ to other libraries that might be using it and force jQuery. This will cause the other library to fail. Maintaining the jQuery is safer.

To do so "controlled"

jQuery(document).ready(function ($) {
        jQuery("#finalizarcadastrojovem").click(function () {
            var form = jQuery("#nome").val();
            console.log(form);
            return false;
        })
});

So you can recover the dollar only within this scope and in the global space the dollar is "available" to be used by other libraries.

Browser other questions tagged

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