I have, for example this:
$first = $('#firstElement').click(function() { });
But in some point of the execution of the page, I will replace that element with another:
$newElement = jQuery('<div />');
$first.replaceWith($newElement);
But I want to keep the callbacks for the first element in the second...
Is that possible?
EDIT 1
I already know how to do it with .on()
or .delegate()
. This is my scenario:
I need to load something once a selector change, however, the content of that selector will vary according to the role of the logged-in user. I already have the information stored in localstorage
. The content of that selector is loaded via:
window.Core.setBranchesSelector($('#theElementInWhichTheSelectorWillBe'));
That function create a <select>
element. I can make within the js
for that page:
$('#parent').on('click', '#theElementInWhichTheSelectorWillBe', function() {});
But I wanted to make that function as flexible as possible, thus, if I make somethink like:
$elementToBeReplaced = $('<coolElement />').click(function() {});
/* Something really cool with that element */
Core.setBranchesSelector($elementToBeReplaced);
The selector inherit the callback for the click
event...