click event to the ColVis list items execute multiple times

click event to the ColVis list items execute multiple times

GurupriyanGurupriyan Posts: 15Questions: 6Answers: 0

Need to add a click event to the ColVis list items.

 $('.ColVis_collection li').click(function () {
alert($(this).text());
});

Working good. Pasted the code in drawCallback function

 drawCallback: function () {
 $('.ColVis_collection li').click(function () {
alert($(this).text());
});
}

this function only works while paste that inside drawCallback function. Problem is click function get execute multiple times for a single click. Kindly do help ASAP

Answers

  • colincolin Posts: 15,237Questions: 1Answers: 2,598

    Hi @Gurupriyan ,

    drawCallback is called on every table draw - this means you're adding an additional click listener every time the table is drawn, which is why you're seeing multiple messages (which will increase with every draw).

    If it's working good, what was the intention of putting it into the callback function?

    Cheers,

    Colin

  • GurupriyanGurupriyan Posts: 15Questions: 6Answers: 0

    Thanks @colin .

    $('.ColVis_collection li').click(function () {
    alert($(this).text());
    });
    

    The above function only get works if we paste inside the drawCallback function . Otherwise not working. Thats why i put inside drawCallback function

  • colincolin Posts: 15,237Questions: 1Answers: 2,598
    edited November 2019

    One thing you could try is to to use on and off, rather than click - something like

    drawCallback: function () {
      $('.ColVis_collection li').off('click');
    
      $('.ColVis_collection li').on('click', function () {
        alert($(this).text());
      })
    }
    

    If that doesn't help, we'd need to see the problem. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

  • GurupriyanGurupriyan Posts: 15Questions: 6Answers: 0

    Thanks again @colin . But this not works. Its killing click event. And Sorry to say that my code need to be confidential as per my organization policy. So unfortunately can't share the code for test case with you. Cheers

  • kthorngrenkthorngren Posts: 21,166Questions: 26Answers: 4,921

    I would put that code in initComplete.

    And Sorry to say that my code need to be confidential as per my organization policy. So unfortunately can't share the code for test case with you.

    For this issue we don't need to see your data. We just need to see a page that has enough of your configuration running to see the click event. You can simulate a small amount of data.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

This discussion has been closed.