Search code examples
jquerycloneaccordion

cloned accordion is not functioning like original accordion


I’m using jquery accordion in a web application. And I want to use this accordion in multiple places. So, I thought of cloning it. But the cloned accordion is only giving me the image of original clone and it’s not functioning like that of original one. The original accordion comprises of selectable items. After selecting one item some operation are carried out. I have written java script code for this operation. I just now want this to happen with other cloned accordion and selectable items too. so, How can I make cloned accordion function like the original accordion?


Solution

  • Use the deep cloning arguments of jQuery clone() to clone with all data and events:

    .clone( [withDataAndEvents] [, deepWithDataAndEvents] )
    

    [withDataAndEvents]: A Boolean indicating whether event handlers and data should be copied along with the elements. The default value is false.

    [deepWithDataAndEvents]: A Boolean indicating whether event handlers and data for all children of the cloned element should be copied. By default its value matches the first argument's value (which defaults to false).

    So do $('your-selector').clone(true, true)

    More here: http://api.jquery.com/clone/