PS: Don't know what was this or objectContainer in OP's question, so created a closure and pasting result in new wrapper.
Solution 3:
The problem is not caused by the event listeners in and of themselves. The issue is how Bootstrap works. For most of the Bootstrap components, Bootstrap creates a JavaScript object associated with the DOM element. This object is what you need to take care of when you clone Bootstrap components. For a collapse element, Bootstrap creates a Collapse object.
The JavaScript object is associated with the DOM element through jQuery's $.data. This is the problem. If you use jQuery's clone and request that event handlers be copied to the clone, then you also get a copy of the data set with $.data. However, when the data is a reference to a JavaScript object, then it is the reference that is copied to the clone. So the original and the clone refer to the same JavaScript object and this is where everything goes astray. This is not special to Bootstrap, by the way: any reference is subject to this problem.
What you could do is perform a $.removeData on the cloned elements that are Bootstrap components. This will force Bootstrap to recreate the JavaScript object. For components that automatically enroll in the data API, this should be all that's needed. (collapse automatically enrolls.) For components that do not automatically enroll (e.g. tooltips), you need to call $.[component] to recreate the component manually.
Add the parameters true, true to var header = objectContainer.clone();
Modify header.find(".collapse").attr("id", collapseId) to add .removeData() at the end.
Solution 4:
I have created a JSFiddle of your code. The most possible error seems to that you where using a this.collapsibleObjCounter++ value of 0 that might be creating an issue.
Here is a working JSFiddle. Please let me know if that is what you are trying to do. Thank you.
If you are having a value of 0 in this.collapsibleObjCounter++ then i would suggest doing ++this.collapsibleObjCounter. Instead of post increment do pre increment
Share
Post a Comment
for "Cloning A Bootstrap Element With An Event Listener"
Post a Comment for "Cloning A Bootstrap Element With An Event Listener"