select2 / select2-bootstrap-theme

A Select2 v4 Theme for Bootstrap 3
https://select2.github.io/select2-bootstrap-theme/
MIT License
871 stars 498 forks source link

Check if select2 script is loaded or not #64

Closed alvinkonda closed 7 years ago

alvinkonda commented 7 years ago

Since im not good in JS im asking here for a bit of help. So how can i load the following code on all pages.

jQuery.fn.select2.defaults.set( "theme", "bootstrap" );

without causing this issue.

theme.js?ver=4.7.3:15 Uncaught TypeError: Cannot read property 'defaults' of undefined

what if statement should i add to my code to check if select2 script is loaded or not because select2 script is loading only on my checkout page

Thanks in advance Alvin

fk commented 7 years ago

It's a bit hard to tell without seeing how exactly you are loading jQuery and Select2, but it seems that you are trying to set the default theme with jQuery.fn.select2.defaults.set( "theme", "bootstrap" ); before loading Select2. You could check the sources of http://select2.github.io/select2-bootstrap-theme/4.0.3.html or provide a JSFiddle or CodePen to receive more help.

fk commented 7 years ago

Sorry – re-read your question. Please try a simple

if (jQuery.fn.select2) {
  jQuery.fn.select2.defaults.set( "theme", "bootstrap" );
}

and report back!

djibe commented 7 years ago

Why don't you use the settings to use Bootstrap theme ? Here is my script, see the last line : $(".select2").select2({ data: biologieExamensListe, tokenSeparators: [',', ' '], tags: true, placeholder: "Examen(s) de biologie", allowClear: true, autoCorrect: "on", maximumInputLength: 20, theme: "bootstrap" });

alvinkonda commented 7 years ago

Hi that simple if statement solve my issue, strange i thought i have already tried in that way and it wasn't worked before. Probably I should learn JS

The problem is that im working with another script that is using select2 (WooCommerce) and i cant change the whole JS code djibe89 mentioned through my theme.

Again thanks

fk commented 7 years ago

@djibe89 Thanks for chiming in! @themedotal Glad your issue is resolved!