Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

89
Vistas
listen for a click outside a cell if this is in edit mode

I have a table and I listen for double click on a table cell. If user double click on a cell that cell becomes editable. I am looking for how to revert the "edit" state once the user clicks outside the cell (anywhere else on the page). The code I have so far is working for entering the "edit" mode:

$('table tbody tr td').dblclick(function(){
    var cell=$(this)
    var value=cell.text();
    var rowIndex = cell.parent().index('table tbody tr');
    var tdIndex = cell.index('table tbody tr:eq('+rowIndex+') td');
    //my logic on editing
    //the event handling if I click outside of that `cell`
});

The code I miss should be triggered only if there is a cell being edited, shouldn't interfere with other click events on the page. How can I accomplish this?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can attach a click event listener to the document that checks whether one of the table cells contains the event target. If not, you know the click event was not directed towards the table cell, and you can perform the logic to disable editing:

const tableCells = $('table tbody tr td')
tableCells.dblclick(function() {
  $(this).attr('contenteditable', 'true').focus();
});

$(document).click(function(e) {
  if (!tableCells.toArray().some(f => f.contains(e.target))) {
    tableCells.removeAttr('contenteditable');
  }
})
td {
  margin: 10px;
  padding: 10px;
  border: 1px solid;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tr>
    <td>Hello</td>
    <td>World!</td>
  </tr>
</table>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda