It's been a long time since I used jQuery, so I'm a bit rusty. Having an issue with a click event on a simple button. (I've tried going through every suggested question that seems like a similar issue. The solutions there don't seem to fix this.) This is a form to change the year for output in a table (data retrieved via ajax, so no page refresh). The user will need to be able to change the year and submit the form multiple times.
<form id="toolbar-year-form">
<input type="text" name="target-year">
<button>Go</button>
</form>
$(document).ready(function() {
$('#toolbar-year-form > button').off().on('click', function() {
$('#toolbar-year-form').submit(function(e) {
e.preventDefault();
e.stopPropagation();
alert("submitted");
});
});
});
If I fill in the input and click the button, it works as expected. If I click it again without a page refresh, the event will then fire twice. If I click again, it fires three times. And so on...
The click event is being bound again each time I click, which explains the multiplication of firing, but I don't know why it's being bound again. :-)
What am I missing?