access a td of the table when clicking

0

I would like to know how I can access a part of my table, the part where I put the label-success I assign an id to td to do something but it does not work as I put the span and the same. I do not know what I'm doing wrong

use this function did not work for me

$( '#idse' ).dblclick(function() {
  alert( "s" );
});
$( '#idven' ).dblclick(function() {
  alert( "v" );
});
$( '#idge' ).dblclick(function() {
  alert( "g" );
}); 

code

for(var i=0; i<resp.length; i++)
{
 var diffTime = moment(fecha).diff(resp[i].fecha_registro);
        var dias = diffTime/86400000;
        console.log(dias);
     var labelType = (resp[i].res == 'aprob') ? 'label-success' : 'label-warning';
     var labelType1 = (resp[i].se == 'aprob') ? 'label-success' : 'label-warning';
     var labelType2 = (resp[i].ven == 'aprob') ? 'label-success' : 'label-warning';
     var labelType3 = (resp[i].ge == 'aprob') ? 'label-success' : 'label-warning';
     var boton = (resp[i].as== '') ? '<div data-id="'+resp[i].id+'" data-nombre= "'+resp[i].nom+'" class="btn btn-primary registrar_o">Asignar</div>' : resp[i].as;

     html+= '<tr><td data-id="'+resp[i].id+'">'+resp[i].fecha_registro+
        '</td><td class="center" data-id="'+resp[i].id+'"><span class="label ' + labelType + '">'+resp[i].res+
        '</span></td><td id="idse" class="center" data-id="'+resp[i].id+'"><span class= "label ' + labelType1 +'">'+resp[i].se+
        '</span></td><td id="idven" class="center" data-id="'+resp[i].id+'"><span class="label ' + labelType2 +'">'+resp[i].ven+
        '</span></td><td id="idge" class="center" data-id="'+resp[i].id+'"><span class="label ' + labelType3 +'">'+resp[i].ge+
        '</span></td></tr>';
}

$('#body').html(html);
    
asked by Juan Jose 12.10.2018 в 03:00
source

1 answer

0

I understand that all the code is in the same JavaScript Sheet, that is, the events assigned to the IDs must be assigned after the assignment of the dynamic HTML to the element.

You can help with functions, that is ...

function _addEventDBLClick() {
    if ($('#idven')) {
      $('#idven').on('dblclick',function(){
        alert( "s" );
      }
   }
  /*
$( '#idse' ).dblclick(function() {
  alert( "s" );
});
$( '#idven' ).dblclick(function() {
  alert( "v" );
});
$( '#idge' ).dblclick(function() {
  alert( "g" );
}); 
  */
}

Calling it at the end after the line:

$('#body').html(html);
_addEventDBLClick();
    
answered by 12.10.2018 в 09:05