Select2: Select2 doesn't work with Cloned <select>

Created on 9 Jul 2014  路  1Comment  路  Source: select2/select2

Hi
I have observed that select2 doesn't work with a cloned tag to more html/css code.
But is there any way I can get this cloned select tags work?

Do you know any workaround for this ?

Most helpful comment

Hi @getvivekv,

I should start off by mentioning that while initializing Select2 against a class work, you will hit problems if you re-initialize Select2 using a class multiple times.

There are two ways of cloning Select2 and still making it work. One is to clone the original <select> and then initialize Select2 on it, and the other is to clone the <select> and Select2 instance after Select2 is initialized.

Initializing Select2 before

This is how you are currently doing it, where you are initializing Select2 and then cloning it. This is not the recommended way as it will most likely cause problems in the future (if not now).

clone()

When cloning elements with event handlers, such as Select2, it's recommended to pass in true as an argument to clone. This will force jQuery to clone the data and event handlers that Select2 has set up.

I understand that this is because select2 convert the tag to more html/css code.

Right, in order to clone Select2 you must clone both the original <select> element and the <div> element that Select2 generates.

Initializing Select2 after

This is the recommended way, where you clone the <select> element and then initialize Select2 afterwards on it.

var original = $(".js-select");
var clone = original.clone();
clone.select2();

This will guarantee that each Select2 instance is unique and will not interfere with each other.

>All comments

Hi @getvivekv,

I should start off by mentioning that while initializing Select2 against a class work, you will hit problems if you re-initialize Select2 using a class multiple times.

There are two ways of cloning Select2 and still making it work. One is to clone the original <select> and then initialize Select2 on it, and the other is to clone the <select> and Select2 instance after Select2 is initialized.

Initializing Select2 before

This is how you are currently doing it, where you are initializing Select2 and then cloning it. This is not the recommended way as it will most likely cause problems in the future (if not now).

clone()

When cloning elements with event handlers, such as Select2, it's recommended to pass in true as an argument to clone. This will force jQuery to clone the data and event handlers that Select2 has set up.

I understand that this is because select2 convert the tag to more html/css code.

Right, in order to clone Select2 you must clone both the original <select> element and the <div> element that Select2 generates.

Initializing Select2 after

This is the recommended way, where you clone the <select> element and then initialize Select2 afterwards on it.

var original = $(".js-select");
var clone = original.clone();
clone.select2();

This will guarantee that each Select2 instance is unique and will not interfere with each other.

Was this page helpful?
0 / 5 - 0 ratings