Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQUERY event firing more than one time, when using delegate jquery

I am using delegate jquery in my code, but when it fire some event it fire more than once, I know It is because i have bind the event of optionclicked class to the boxes-main class but i am in a situation that i have to bind these classes with each other, because the content that using optionclicked class is generating dynamically. Is there ay solution to the problem so that event fire only once means it call function once and show pop op once and post data once etc etc.

$('.boxes-main').on('click', '.optionclicked', function(){
    // do something
    var timetoclick = parseFloat($('#time').text());
    clearInterval(myCounter);
    var optionclicked = $(this).attr('data');
    var questionid = $(this).attr('data2');
    $.post("quesanscheck.php", {
        ans : optionclicked,
        id : questionid,
        time : timetoclick
    }, function(data, status) {
        alert(data);
        if(data == optionclicked)
        {
            //alert("dfad");
            setTimeout(function(){ rightans(); }, 1000);
        }
        else
        {
            // wrong ans red
            if(optionclicked == 'A')
            {
                document.getElementById('op1').style.background = "red";
                document.getElementById('op1').style.border = "0px";
            }
            if(optionclicked == 'B')
            {
                document.getElementById('op2').style.background = "red";
                document.getElementById('op2').style.border = "0px";
            }
            if(optionclicked == 'C')
            {
                document.getElementById('op3').style.background = "red";
                document.getElementById('op3').style.border = "0px";
            }
            if(optionclicked == 'D')
            {
                document.getElementById('op4').style.background = "red";
                document.getElementById('op4').style.border = "0px";
            }
            // right ans green
            if(data == 'A')
            {
                document.getElementById('op1').style.background = "green";
                document.getElementById('op1').style.color = "white";
                document.getElementById('op1').style.border = "0px";
            }
            if(data == 'B')
            {
                document.getElementById('op2').style.background = "green";
                document.getElementById('op2').style.color = "white";
                document.getElementById('op2').style.border = "0px";
            }
            if(data == 'C')
            {
                document.getElementById('op3').style.background = "green";
                document.getElementById('op3').style.color = "white";
                document.getElementById('op3').style.border = "0px";
            }
            if(data == 'D')
            {
                document.getElementById('op4').style.background = "green";
                document.getElementById('op4').style.color = "white";
                document.getElementById('op4').style.border = "0px";
            }
            setTimeout(function(){wrongans();}, 1000);
        }
    });
}); 
like image 337
Naveen Singh Avatar asked Oct 30 '22 15:10

Naveen Singh


2 Answers

I had a similar problem.

try changing your code to:

$('.boxes-main').unbind('click').on('click', '.optionclicked', function(){

This removes all click events and adds a single event.

When I looked into this, the click event was firing twice, even though the click event was only added once.

I can't find the original SO post that helped me, but this worked for me.

like image 106
Karl Gjertsen Avatar answered Nov 12 '22 16:11

Karl Gjertsen


I binded the click event with an variable named event and added the code

event.stopPropagation();
 event.preventDefault();

this one solved my problem.

like image 33
Naveen Singh Avatar answered Nov 12 '22 16:11

Naveen Singh