javascript - Click function still working for previous class which is already replaced by other -
i having problem jquery click event. when replace class of div
click function still working previous class. here code working with:
$(this).removeclass('edit_agent_val').addclass('edit_agent_val2');
the click function still working edit_agent_val
instead of edit_agent_val2
.
// should not work $('.edit_agent_val').on('click', function(e) { // code... }); // should work $('.edit_agent_val2').on('click', function(e) { // code... });
can me fix issue?
the issue because events attached on load when element still has original class. act of changing class attribute not affect of events attached element.
to achieve want need use delegated event handler, attached parent element:
$(document).on('click', '.edit_agent_val', function(e) { // code... }); $(document).on('click', '.edit_agent_val2', function(e) { // code... });
note have used document
here example. in final working code should use closest parent element both .edit_agent_val
elements.