[Dropdown] Replacing dropdown breaks click event
Steps to reproduce
- Have a dropdown
- On change replace the dropdown and add new clickable elements
Expected result
I can click the clickable elements, such as a button
Actual result
Button is not clickable - it gets caught in event.preventDefault in dropdown code after eventInModule call.
Testcase
https://jsfiddle.net/fpr4joez/4/
Version
Latest
Solutions
I have none to offer but am interested to hear your viewpoint. I do have a listener directly on the select since it is another universal library to which we would prefer not to introduce this UI's code. However, even if that is the case - what is the appropriate methods to call, steps to take? Registering the same functionality in dropdown's onChange method does not change anything - the button still doesn't work.
Just some thoughts on the issue, I noticed that the log has the error outlined below in the screenshot.

I realised something happened when you removed the dropdown from the dom when it did not finish animating. With that in mind I modified it and instead hid the dropdown until you replaced the wrapper element completed. This allows the other event to fire as you expect (https://jsfiddle.net/princeandrew01/0zyL8uoj/27/)
Additionally, I try not to remove elements like this from the dom unless absolutely necessary. I make modifications instead and add if anything. I am curious as to what scenario this will be applicable to what you are doing if you have time to explain.
EDIT: Grammer and spelling
With my code it is not possible to hide it - it replaces the entire contents. And honestly replacing parts of the DOM is normal. Pretty much everything loaded via AJAX replaces things in DOM. And cycling through all of the replacable content to make sure you close or destroy or do something else action so that everything else doesn't break doesn't sound right to me.
In my scenario, as you can guess from the fiddle, choosing something from the dropdown changes some things and it needs to load new content, basically. Part of what it changes is adding a button in a form that then doesn't work.
I know it is cause of the event.preventDefault that didn't exist in Semantic and so I had to comment it out in the source. I just don't understand the why behind it - why does it stop processing the form submit. It is not related to the dropdown. It's a completely different element.
Understood about the ajax part and I agree that its sometimes better to replace it instead of worrying about stuff. Its always a give and take when dealing with User interfaces.
However back to your original error it has nothing to do with the Dropdown object. What it has to do with is the screenshot i posted previously. The close animation is not completely done before you replace the content in the wrapper. With that in mind I looked up the animation time and its 200 ms so I set a timeout for 250 ms before replacing the content. Your submit button now works.
https://jsfiddle.net/princeandrew01/0zyL8uoj/42/
Exactly, it has nothing to do with it, but it does. On click it goes through dropdown functions. I am guessing because if you click elsewhere then it is tries to close any opened dropdowns, acting like a select box would. However when encountering that the dropdown is missing, it breaks - it doesn't allow it through, it calls event.preventDefault when it shouldn't.
I looked through some more of the dropdown functions and there is a onHide (fires before the it is hidden). It all comes down to the error the transition is firing and stopping the submit from going through. I tried suppressing it with the silent flag and it still threw the error.
My only recommendations would be:
- A temporary solution to wait for the animation to finish before removing the dropdown (there is a is hidden check on the dropdown).
- A more permanent solution is adding an onHidden callback so the dropdown can be replaced after it finishes doing the transition.
EDIT: Grammar and spelling
The preventDefault was introduced in #188 to swallow the toggle event.
We'll investigate.
Fixed by #891 See your adjusted jsfiddle here https://jsfiddle.net/m51q6oje/