I want to add a class to a button when clicking first time, and when clicking again on the newly added class, it should do something (for example show an alert).
I'm trying this, but this shows the alert on the first click, even though the class doesn't exist at first.
HTML:
<div id="banner-message">
<p>On first click, add .added class to the button</p>
<p>On clicking .added, show alert</p>
<button>Click</button>
</div>
jQuery
$('button').on('click', function(e){
e.preventDefault();
$(this).addClass("added");
})
$(document).on('click', '.added', function(e){
e.preventDefault();
alert("Clicked on .added");
})
JSFiddle: https://jsfiddle.net/n837rw5h/