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

122
Vistas
HTML Table created with Javascript not listening to CSS and Scripts

I have the following code which represents an HTML Table and after button onClick I create one more same HTML Table.

The problem is that the second table is not listening on the Function applied to the first table as well that highlights the cells.

I am trying to figure out if there is something that preventing the second table to listen to the script code as well.

Any ideas would be much appreciated.

The style CSS is :

         table,tr,th{
         border:2px solid black;
         }
         table{
         width:200px;
           height:200px;
         }
         th.highlight {
         background-color: black;
          } 

The HTML Table looks like this :

         <table id="test">
         <tr><th>test</th><th>test</th><th>test</th></tr>
         <tr><th>test</th><th>test</th><th>test</th></tr>
         <tr><th>test</th><th>test</th><th>test</th></tr>

         </table>
         <div id="test2">

         </div>
         <button type="button">Add</button>
        

And the Script that applies to the first table but it is not working on the second :

    const addColumn = () => {
    var x = document.getElementById("test2");
    x.innerHTML = "<table> <tr><th>test2</th><th>test2</th><th>test2</th></tr> <tr> 
    <th>test2</th><th>test2</th><th>test2</th></tr> <tr><th>test2</th><th>test2</th> 
    <th>test2</th></tr></table>";
    }

    document.querySelector('button').onclick = addColumn
    $('th').hover(function() {
     $(this).addClass('highlight');
     
 }, function() {
     $(this).removeClass('highlight');
 
 });
about 4 years ago · Juan Pablo Isaza
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