I have a <select>
form element that is being generated dynamically after the page loads. I was using jQuery to attach a change
event that would update other fields in the form, and wanted to eliminate the need to rewire the handler each time the form containing the <select>
was replaced.
For click
events on dynamic elements, I have been moving to unobtrusive JavaScript and wanted to use that approach for change
- but no success. Attaching to the <select>
directly was cumbersome because when the node is replaced in the DOM, the connected event listener dies with it, and the new node must be attached to again.
So I wanted to know, is there a way to unobtrusively assign a change
event handler to the parent element of a newly-created <select>
and let the event bubble up to a handler on a previously-existing element, in order to avoid attaching a handler each time a new target input element is created?