$ from jQuery does not work

2

I'm using a code that uses $ of jQuery, but when using the dollar sign it's error on the console and the jQuery code does not work. 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 would like to know how to fix this, since I want to use $ and not jQuery     

asked by anonymous 16.07.2014 / 02:46

3 answers

5

You should 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 look in the scripts and try to find this jQuery force, if you have nothing I do not know what to say, I use jQuery.noConflict () quite a bit, at a glance link

    
16.07.2014 / 02:57
5

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

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

There are several ways to restore the dollar.

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

To force globally you can do:

window.$ = jQuery;

Please note that this removes $ from other libraries that might be using it and force jQuery. This will cause the other library to fail. Keeping jQuery is safer.

To do it "in control"

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 global space the dollar is "available" for use by other libraries.

    
16.07.2014 / 08:29