I have a button that calls a function, and opens the modal at the same time. The first time I do it, it does it well but the second time I run the button, I see this error in the console:
Type error: can not read property parentnode of null.
I leave the code.
This is the button in html, I open the modal.
<button type="button" id ="btnConvModal" class="btn btn-primary btn-xs " data-toggle="modal" data-target="#modalConvenios"><strong>Ver Convenios</strong></button>
I call the function
$("#btnConvModal").on( "click", function() {
busConveniosModal();
});
This is the function:
function busConveniosModal(){ //Funcion que me lista una tabla con los convenios en un formulaio modal de administrador.
$('#modalConvenios').on('shown.bs.modal', function (e) {
tblConvenios = $('#tblConvenios').DataTable({ //Voy a necesitar la variable tabla
"responsive": {
"details": false,
},
autoWidth: false,
"processing": true,
"pagingType": "simple", //Hace que se muestren todos los elementos de la paginación
"destroy": "true", //Esto lo hago para que no me de error el datatable cuando lo vuelvo a inicializar una vez cargado.
"language": {
"url": "https://cdn.datatables.net/plug-ins/1.10.11/i18n/Spanish.json"
},
"ajax": {
"url": "php/AgrApp/cnv_emp.php", //web a la que llamo y hace el trabajo.
"type": "POST", //Metodo usado por la funcion para pasar variable
"dataSrc": "",
},
"columns": [
{"defaultContent":" <button type='button' id=\"idConvenioAdmin\" class='selectConv btn btn-primary btn-xs '><span class='glyphicon glyphicon-plus'></span></button>","width": "3%"},
{ "data": "id" },
{ "data": "name" },
]
});
});
}
By making an alert ("test"); When I opened the modal, I realized that the second time I opened it, I ran the alert twice. I've put this line
$('#modalConvenios').off('shown.bs.modal');
and the problem has been solved, what I do not understand is the reason of the problem, it is how they were adding or storing $('#modalConvenios').on('shown.bs.modal', function (e) {
every time I call it.