[jQuery] Multiple AJAX calls problem
Hello. So I have a problem that when I get jQuery code in ajax
response and you click few times to activate ajax call, there are
multiple instances of same code. Lets see the example with keydown.
FILE 1:
<script>
$(document).ready(
function() {
$("#my_div").click(function() {
$.ajax({
type: "GET",
url: "test.php",
success: function(data) {
$("#div_ajax").html(data);
}
})
})
}
)
</script>
<div id="my_div">Click here</div>
<div id="div_ajax"></div>
FILE 2 - AJAX RESPONSE:
<script>
$().keydown(function(event) {
alert(event.keyCode);
});
</script>
Ajax content loaded...
So, if I click on my_div twice, there will be two listeners for keys
and for same keydown two alerts.
I can solve this issue with ifs, but what is the best way to prevent
this behaviour? Maybe in the way of singletons or something?
Thanks for any advice, I am sure there must be simple and effective
solution. Have a nice day.