Select2 does not work on my pc but works on jsfidle

Asked

Viewed 460 times

0

This jsfidle code works, i.e., shows stylized select and select with input search.

    <html>
            <head>
                <title>TESTE</title>
                <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

                <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/i18n/pt-BR.js"></script>
                <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.js"></script>

                <script language="JavaScript">
                    $('.multiplo').select2({
                        placeholder: 'selecione'
                    });
                </script>

                <style type="text/css">
                    .multiplo{
                        width:50%;
                    }
                </style>
            </head>


            <body>

            <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/css/select2.css" rel="stylesheet"/>

            <select class="multiplo" name="" multiple="multiple">
                <option>Um</option>
                <option>Dois</option>
                <option>Três</option>
                <option>Quatro</option>
            </select>



            </body>
            </html>

But when I get this code and play in a file. php on localhost does not work.

How it appears when running in jsfidle: Execução no jsfidle

Now as it appears when running on localhost: Execução no localhost

Can anyone tell what might be causing this?

In Chrome console sources it seems that it loads both . css and . js

Source console chrome

EDIT

Console tab Aba console

  • As I said earlier, put the css link inside the <"head"> tag, delete cache (optional) and try again! In your case, it could be cache issues that are quite common! Keep us informed!

1 answer

2


You can only access page elements after it is fully loaded.

For this put your code inside:

$( document ).ready(function() {
    //aqui
});

Or in its most compact and common form:

$(function() {
    //aqui
});

Applying to your code would look like this:

$(function() {
    $('.multiplo').select2({
        placeholder: 'selecione'
    });
});

Jsfiddle does not need to have this because the platform itself runs only the javascript block after html and css is loaded. The same happens in live snippets stackoverflow.

Documentation for Document ready de Jquery

  • Poxa...that’s right!!! I didn’t even think about it. Vlw brother!!! What was occurring then was that the pages were loading the DOM before loading the scripts . js and . css?

  • @L.J Otherwise, the pages were running js before loading the DOM.

  • Js is always loaded before the DOM ?

  • @L.J Depends on where you are on the page. Usually you are at the top, on <head> then it runs before the DOM is loaded. If you want to make sure it runs only after the DOM has been loaded you have to put the code inside the Document ready as I indicated.

Browser other questions tagged

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