Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

111
Views
Agregar y eliminar elemento html por jquery

¿Cómo puedo agregar un nuevo <tr> después de hacer clic en <tr> ? ¿Al hacer clic en este <tr> y eliminar ahora creado al hacer clic en otro <tr> (con repetir el primer escenario) por jQuery?

Intenté así, pero no funciona:

 $("#test_table tr").click(function() { $(this).prev().remove(); $(this).closest('tr').append('<tr><td>2.2</td></tr>'); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table id="test_table"> <tr> <td>1</td> </tr> <tr> <td>2</td> </tr> <tr> <td>3</td> </tr> </table>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Dado su objetivo, parece que desea reemplazar el tr actual con el nuevo fragmento de código HTML. Para hacer eso, solo necesita llamar a after() para insertar el nuevo HTML, luego remove() para borrar el tr original:

 $("#test_table tr").click(function() { $(this).after('<tr><td>2.2</td></tr>').remove(); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table id="test_table"> <tr> <td>1</td> </tr> <tr> <td>2</td> </tr> <tr> <td>3</td> </tr> </table>

Sin embargo, como el único efecto de esto es cambiar el texto dentro de la td , puede simplemente llamar a text() en su lugar:

 $("#test_table tr").click(function() { $(this).find('td').text('2.2'); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table id="test_table"> <tr> <td>1</td> </tr> <tr> <td>2</td> </tr> <tr> <td>3</td> </tr> </table>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!