Last active
May 26, 2021 07:03
-
-
Save bartlomiejmatlega/66ea6bcec658202677e1b81c49bd42ce to your computer and use it in GitHub Desktop.
Turbolinks and Select2 - duplicated select problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// destroy select2 before caching | |
$(document).on("turbolinks:before-cache", function () { | |
if ($('.select-class').first().data('select2') != undefined) $('.select-class').select2('destroy'); | |
}); | |
// initialize select2 on turbolinks:load | |
$(document).on("turbolinks:load", function () { | |
$('.select-class').select2(); | |
}); |
document.addEventListener("turbolinks:load", function() {
$('.select2-container').remove() //remove the select2 container DOM
$('.select2-input').select2(); // Select2 will just reinit the DOM
})
Thanks alot! only suggestion that worked for me after hours of search
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It worked! Thanks.