How to clone select2 in jquery?
select2(); $(‘#add_country’). on(‘click’, function () { count++; var sel = $(‘#ws_language’); var clone = sel.clone(); clone.attr(‘id’, ‘newId’+count); clone.
Table of Contents
How to solve select2 jquery not working after cloning?
What worked for me, to clone the select2 managed select input I did the following:
- Destroy the selection that is cloned.
- Clone with a true param.
- Remove from clone attributes ‘id’ and ‘data-select2-id’
- Remove the ‘data-select2-id’ attribute from each option in the clone.
- Reinitialize the element that was cloned.
How do I destroy select2 and reset?
- remove from select 2.
- select 2 options.
- $(“select”).select2(“destroy”);
- open javascript select2.
- create select2 when destroy.
- kill select2.
- Select 2 open at the bottom.
- select2 click close event.
What is Select2()?
Select2 gives you a customizable select box with support for search, tagging, remote datasets, infinite scroll, and many other widely used options.
Why doesn’t Select2 work with jQuery clone?
When cloning elements with event handlers, such as Select2, it is recommended that you pass true as the argument to clone. This will force jQuery to clone the data and event handlers that Select2 has set up. I understand this is because select2 converts the tag to more html/css code.
Is there a way to clone a selected item?
Correct, to clone Select2 you need to clone both the original element and the element that Select2 generates. This is the recommended way, where you clone the element and then initialize Select2 on it.
Do you pass true to a clone in Select2?
When cloning elements with event handlers, such as Select2, it is recommended that you pass true as the argument to clone. This will force jQuery to clone the data and event handlers that Select2 has set up.
How does the .clone method work in jQuery?
By default, its value matches the value of the first argument (which defaults to false). The .clone() method performs a deep copy of the set of matching elements, which means that it copies the matching elements, as well as all their descendant elements and text nodes.