Capturing table elements with jquery

-4

How can I do to grab the elements of a table that is being populated dynamically with jQuery? I want it when the clicked pain is marked only 1 row of the table. I just managed to check them all.

Here when I click on the table it shows a button and now I wanted to when I click on the td it just mark the one that was clicked and not all

code:

$("#registros").click(function(){
  $("#excluir").toggle();
     $("tr").toggleClass("selected");
});
    
asked by anonymous 26.09.2015 / 20:57

2 answers

0

You need to use jquery .on ( link ), going from an element that already exists in the DOM as a reference, for example:

$('#elemento').on('click', '<identificador do elemento dinamico que será clicado', function(){
  // executa função
  });
<div id="elemento">
<!-- aqui vai ser inserido o conteudo dinamico -->
</div>
    
26.09.2015 / 22:18
0

I've done an example running here, see if it helps:

$("table td").on('click', function(e){
  var linha = $(this).parent();
  if($(linha).hasClass("marcado"))
  {
    $(linha).removeClass("marcado");
    $(linha).css('background', "");
  }
  else
  {
    $(linha).addClass("marcado");
    $(linha).css('background', "gray");
  }
  e.preventDefault();
});
table td{
  border: solid 1px #000;
  cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <td>Coluna</td>
    <td>Coluna</td>
    <td>Coluna</td>
    <td>Coluna</td>
    <td>Coluna</td>
    <td>Coluna</td>
  </tr>
  <tr>
    <td>Coluna</td>
    <td>Coluna</td>
    <td>Coluna</td>
    <td>Coluna</td>
    <td>Coluna</td>
    <td>Coluna</td>
  </tr>  
  <tr>
    <td>Coluna</td>
    <td>Coluna</td>
    <td>Coluna</td>
    <td>Coluna</td>
    <td>Coluna</td>
    <td>Coluna</td>
  </tr>  
</table>
    
08.12.2015 / 19:31