I have a button. when I click it I am appending some buttons to the DOM.
The issue I have is that those buttons that I am appending fire multiple times.
$(el).on('click', function (e) {
key();
});
function key() {
$(document).on('click', '#key li', function () {
console.log($(this));
});
}
First time key()
is called, the console.log
fires once
The second time I call key()
the console.log
fires twice
And so on
I've tried adding $(document).find('#key li').unbind('click')
, but that doesn't seem to work
Any ideas?
edit:
Here is an jsfiddle example (shown below).
$('button').on('click', function () {
$('.cont').remove();
$('.container').remove();
var html = '<button class="cont">click</button><div class="container">placeholder</div>';
$('body').append(html);
key();
});
$(document).on('click', '.cont', function () {
var html = '<div id="but_placeholder"><button class="one">1</button><button class="two">2</button><button class="three">3</button></div>';
$('.container').html(html);
});
function key() {
$(document).on('click', '#but_placeholder button', function () {
$('input').val($('input').val() + $(this).html());
});
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="input" />
<button>test</button>
To reproduce, click on the test
button, then on the click
,then one 1
2
3
and repeat the process
You will notice that the second time you go through the process the text doubles
el
? – Theme