I solved this:
- placing an event listener on the parent element
- removing a class from a link ONLY when the user confirms
- link rollback after class removal.
function async() { var dfd = $.Deferred();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container"> <a href="#" class="another-page">Somewhere else</a> </div>
The reason I added an event listener to the parent element, and not to the link itself, is because the jQuery on event will bind to the element until it says otherwise. Therefore, even if an element does not have another-page class, it still has an event plug-in, so you should use event delegation to solve this problem.
GOTCHAS , it is very condition based. that is, if you need to ask the user EVERYTIME, they click on the link, you will have to add a second listener to read the another-page class back to the link. i.e:.
$('body').on('click', function (e) { $(e.currentTarget).addClass('another-page'); });
note that you can also remove the event listener on container , if the user accepts, if you do, make sure you use namespace events, because there may be other listeners in the container that you can accidentally delete. See https://api.jquery.com/event.namespace/ for more details.
devkaoru May 20 '15 at 12:27 a.m. 2015-05-20 00:27
source share