1

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');
    }
});
8
  • There's probably a fancy way, but you could use .one() instead of .on so that it only happens once, or you could disable the button until ajax has completed, or just have a flag eg if(running===true) return; running=true; which you clear in $.ajax().always() Commented Jan 21, 2021 at 9:02
  • Thank you for your help. I am using the click for adding and removing so the ``` .one() ``` is not for this right? how can i disable or add a flag? you can add here? jsfiddle.net/amiralamkhan/b9omyx1g/10 Commented Jan 21, 2021 at 9:09
  • If you want to use .one() (it's not very good UX in this case without additional styling, but fixes your problem) and be able to click it again then you need to add it back using another .one() in the .always() Commented Jan 21, 2021 at 9:10
  • I don't like the .one() can you show me the code for disable the button or add flag? on my jsfiddle? Commented Jan 21, 2021 at 9:13
  • Had a closer look at your code and you're essentially already adding a flag with $('.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. Commented Jan 21, 2021 at 9:14

1 Answer 1

1

Fixed.. I just add multi flags like this and it is working fine and ignoring double clicks.

if(!$('.srcbx').hasClass('opened') && !$('form').hasClass('search')){}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.