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

64
Vistas
Copy row from any table

I have an HTML page with 3 tables on it. I want to be able to copy specific cells in a table row to the clipboard. The row could come from any of the 3 tables.

Using the code below, I highlight and copy the row for a table with an ID of "final". How do I make this work for any of the 3 tables? I tried by getElementsByTagName and labeling them the same name but did not work - understandably so. Is there a way to designate the selected table? I am trying to avoid copying the whole row and might eventually add the formatted msg to a new page rather than copy to the clipboard.

function copy_highlight_row() {
    var table = document.getElementById('final');
    var cells = table.getElementsByTagName('td');

    for (var i = 0; i < cells.length; i++) {
        // Take each cell
        var cell = cells[i];
        // do something on onclick event for cell
        cell.onclick = function () {
            // Get the row id where the cell exists
                
            var rowId = this.parentNode.rowIndex;
            var rowsNotSelected = table.getElementsByTagName('tr');
            for (var row = 0; row < rowsNotSelected.length; row++) {
                rowsNotSelected[row].style.backgroundColor = "";
                rowsNotSelected[row].classList.remove('selected');
            }
            var rowSelected = table.getElementsByTagName('tr')[rowId];
            rowSelected.style.backgroundColor = "yellow";
            rowSelected.className += " selected";
            var cellId = this.cellIndex + 1

            msg = 'Title: ' + rowSelected.cells[0].innerHTML;
            msg += '\r\nDescription: ' + rowSelected.cells[1].innerHTML;
            msg += '\n\nLink: ' + rowSelected.cells[2].innerHTML;
            msg += '\nPublication Date: ' + rowSelected.cells[3].innerHTML;
            //msg += '\nThe cell value is: ' + this.innerHTML copies cell selected
            navigator.clipboard.writeText(msg);
            
        }
    }

};

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

0

Based on a couple of the suggestions I came up with the following:

function highlight_row() {
    
    var cells = document.querySelectorAll('td')
    for (var i = 0; i < cells.length; i++) {
        // Take each cell
        var cell = cells[i];
        // do something on onclick event for cell
        cell.onclick = function () {
            // Get the row id where the cell exists
                
            var rowId = this.parentNode.rowIndex;
            var t_ID = this.closest('table').id 
            var table=document.getElementById(t_ID);
            var rowsNotSelected = table.getElementsByTagName('tr');
            
            for (var row = 0; row < rowsNotSelected.length; row++) {
                rowsNotSelected[row].style.backgroundColor = "";
                rowsNotSelected[row].classList.remove('selected');
            }
            var rowSelected = table.getElementsByTagName('tr')[rowId];
            rowSelected.style.backgroundColor = "yellow";
            rowSelected.className += " selected";
            var cellId = this.cellIndex + 1

            msg = 'Title: ' + rowSelected.cells[0].innerHTML;
            msg += '\r\nDescription: ' + rowSelected.cells[1].innerHTML;
            msg += '\n\nLink: ' + rowSelected.cells[2].innerHTML;
            msg += '\nPublication Date: ' + rowSelected.cells[3].innerHTML;
            navigator.clipboard.writeText(msg);             
        }
    }
}

At the end of the html I call the function. The HTML contains 3 tables and this enabled me to click on any table, highlight the row, and copy the correct range of cells.

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