danielstocks / jQuery-Collapse

A lightweight (~1kb) jQuery plugin that enables expanding and collapsing content
http://webcloud.se/jQuery-Collapse/
MIT License
679 stars 153 forks source link

Refresh collaspe after adding elements to DOM #103

Closed sderrick closed 3 years ago

sderrick commented 4 years ago

I need to add new elements to the collaspe set dynamically. How do I refresh the set so the new elements will expand and collaspe?

danielstocks commented 4 years ago

hi @sderrick

The only way to do get the new DOM elements is to the re-run the plugin.

I guess a refresh method would be a nice addition? API proposal:

var demo = new jQueryCollapse($("#demo")); // Initializing plugin
$("#demo").append(newDomElements)
demo.refresh(); // Add new DOM elements to plugin

Or maybe there is a way for the plugin to listen for a DOM mutation event and automatically refresh itself?

https://stackoverflow.com/questions/2844565/is-there-a-javascript-jquery-dom-change-listener

Suggestions?

sderrick commented 4 years ago

Daniel,

Is it OK to rerun the plugin?

What would be the difference between rerunning it and refreshing it?

thanks,

Scott

On 9/6/2020 11:36 PM, Daniel Stocks wrote:

hi @sderrick https://github.com/sderrick

The only way to do get the new DOM elements is to the re-run the plugin.

I guess a |refresh| method would be a nice addition? API proposal:

|var demo = new jQueryCollapse($("#demo")); // Initializing plugin $("#demo").append(newDomElements) demo.refresh(); // Add new DOM elements to plugin |

Or maybe there is a way for the plugin to listen for a DOM mutation event and automatically refresh itself?

Suggestions?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/danielstocks/jQuery-Collapse/issues/103#issuecomment-688070106, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAFSE7EQX5H552LL7SIBLDTSER5P3ANCNFSM4Q4OM6IQ.

danielstocks commented 4 years ago

I think re-running the plugin could work for your use-case, but there might be un-intended consequences like events firing twice. Here is the code that is run during initialization: https://github.com/danielstocks/jQuery-Collapse/blob/master/src/jquery.collapse.js#L15.

The best thing would probably be to have some kind of refresh method as the browser support for https://caniuse.com/mutationobserver isn't great, and a lot of people are using this plugin for legacy applications.