I am using on click ajax call for take html from a file to append To a div and then on click to remove it by class. The single click is working fine inserting and removing but when by mistake double clicking or on double clicking the html is coming duplicate. Also add it on jsfiddle
here is my jQuery code.
$('.srcbx').on('click',function(){
if($('.srcbx').hasClass('opened')){
$("form.search").slideUp(1500,function(){$(this).remove();});
$(".srcbx").removeClass("opened");
}
else
{
$.ajax({
type:"GET",
url:"<?php echo BASE_URL; ?>/load.php",
data:{action:'search-html'},
success:function(data){
$(data).appendTo("header .container").slideDown(1500,function(){$(this).show();});
$("#search").keyup(function(){
if($(this).val() != '' && $(this).val().length >= 3){
$.ajax({
type:"POST",
url:"<?php echo BASE_URL; ?>/load.php",
data:{action:'search',keyword:$(this).val()},
beforeSend:function(){
$("form.search").prepend("<div class='loader'></div>");
},
success: function(data){
$("#suggesstion").show();
$("#suggesstion").html(data);
$("form.search .loader").remove();
}
});
}
else
{
$("#suggesstion").hide();
}
});
$("#search-btn").click(function(){
var v = $("#search").val();
window.location.href="<?php echo BASE_URL; ?>/?s="+v;
return false;
});
}
});
$('.srcbx').addClass('opened');
}
});
.one()instead of.onso that it only happens once, or you could disable the button until ajax has completed, or just have a flag egif(running===true) return; running=true;which you clear in$.ajax().always().always()flagwith$('.srcbx').addClass('opened');. So I'm not exactly sure where in your code your problem is. I already (perhaps incorrectly) made some assumptions such as assuming "double click" meant "click twice" (which is subtly different) - but do you mean multiple keyups in #search? It's great you've included some code, but perhaps you could reduce your code to only the relevant parts and remove anything that's not relevant.