0
$("#btnClients").click(function () {
            var idx;
            var stable = $('#Tbody1').DataTable({
                "ajax": {
                    "url": '/home/selectCustomers',
                    "type": "get",
                    "datatype": "json",
                    "data": { "critere": txtcr, "ch": txtch },
                },
                "columns": [
                    {
                        "data": "code", "autoWidth": true, "render": function (data) {
                            idx = data;
                            return '<h6 id="' + data + '">' + data + '</h6>';
                        }
                    },
                ]
            });
        });
$(document).ready(function () {
        $("h6").dblclick(function () {
            alert("You have clicked this twice.");
        });
    });

I have some actions, which should occur on a double-click event on h6. However, I tried with this code but it doesnt work. Any other way than this is appreciated.

2 Answers2

0

You need to use event delegation syntax for your event bindings as your h6 is dynamically created.

$('#Tbody1').on("dblclick", "h6", function () {
    alert("You have clicked this twice.");
});

You can delegate the event to the closest static parent or to document/body as these are available at the page load.

Jai
  • 74,255
  • 12
  • 74
  • 103
0

The h6 element is dynamically added after the $(document).ready, so the eventhandler isn't being set.

You need to use $(document).on("dblclick","h6",function (...)); or to set the event after the h6 is added to the DOM.

Programmer
  • 1,973
  • 1
  • 12
  • 20