jquerytwitter-bootstrapbootstrap-modaljquery-widgets

Jquery functions are called twice


Edited

I am opening a modal on click of link in jqwidget grid column, I am using the cell renderer to accomplish this. but the data is rendering twice. Below is a screenshot after clicking to open the modal.

enter image description here

$(document).ready(function () {
   var loadGrid = function (fromDate = null, toDate = null) {
    $.ajax({
        url: "/Home/SearchResults",
        dataType: 'json',
        beforeSend: function () {
            $("jqxgrid").html('');
        },
        error: function (json, textStatus, errorThrown) {
            alert(' Error :' + errorThrown);
        },
        data: {
            fromdate: fromDate,
            toDate: toDate
        },
        success: function (response) {
            // initailize grid
            var gridData = response;
            window.searchData = response.SearchResults;
            var gridSource =
            {
                localdata: gridData,
                datatype: 'json'
            };
            var gridDataAdapter = new $.jqx.dataAdapter(gridSource);

            $("#jqxgrid").jqxGrid(
                {
                    width: 1500,
                    source: gridDataAdapter,
                    pageable: true,
                    autoheight: true,
                    pagesize: 20,
                    selectionmode: 'singlecell',
                    columns: [
                        { text: 'Edit', datafield: 'Edit', width: 250, columntype: 'button', cellsrenderer: function () {
                                return `<a href='' data-toggle='modal' data-target='#JsonModal'  id='elem' /> Get Json`},
                            buttonclick: function (row) {
                                                 editrow = row;
                                                 var offset = $("#jqxgrid").offset();
                                                 var dataRecord = $("#jqxgrid").jqxGrid('getrowdata', editrow);
                                                 selectedFirstName = dataRecord.FirstName;
                            }
                        }
                    ]
                });
        }
    });
}
$(document).on("hidden.bs.modal", "#JsonModal", function () {
    $(this).find("#modal-content").html(""); // just clear the contents.
    $(this).find("#modal-content").remove(); // remove from dom.
});

$('#JsonModal').on('shown.bs.modal', function () {
    $('#JsonModal').find('.modal-body').append('<div id="modal-content">' + selectedFirstName + '</div> ');
});
});

Update:

As pointed out by a couple of commenters, I noticed my jquery functions are being called twice.

 $(document).on('click', 'a[id^="elem"]', function () {
        alert('hello');
    }); 

an alert box is open is opened twice. I am unclear why.


Solution

  • Check your page to make sure that you aren't including jQuery twice on the page.

    This is a very common mistake and can result in the problem that you describe in your question of an event being called/rendered twice.