Get value from a cell in jquery

-2

ThisbuttonhasanONCLICKeventthatcallsafunction.Theoretically,itisalreadyinaline,itwouldonlyneedtheVALUEofacell.

obs:AddressesisthetbodyIDandtheTESTistheCELLID.Currentfunction:

functionteste(){varid=$(this).parent().find('td').attr('id');alert(**enderecos**[1].**teste**);}

Functiontorecoverdataonthescreen:

functionatualiza(){$.ajax({dataType:'json',url:'get_enderecos.php',success:function(data){for(vari=0;data.length>i;i++){$('#enderecos').append('<tr><tdid="teste">'+data[i].sequencia+'</td>'+
                                                    '<td>'+data[i].cep+'</td>'+
                                                    '<td>'+data[i].endereco+'</td>'+
                                                    '<td>'+data[i].bairro+'</td>'+
                                                    '<td>'+data[i].cidade+'</td>'+
                                                    '<td>'+data[i].estado+'</td>'+
                                                    '<td class="actions col-xs-2 col-sm-2 col-md-2 col-lg-2" align="center">'+
                                                        '<button class="btn btn-danger" onClick="teste()" type="button">Alterar Endereço</button>'+
                                                     '</td></tr>');

                            }
                        }
                    });

                }
    
asked by anonymous 26.10.2017 / 04:26

1 answer

0

I start by saying that the id you are putting in the first <td> of the row is not correct because it will generate multiple rows with ids repeated. In html the id attribute must be unique on the page.

Citing the W3C documentation : / p>

  

The value must be unique amongst all the IDs

Translating:

  

The value must be unique among all IDs

To fix this problem you can turn it into a class by changing append to:

$('#enderecos').append('<tr><td class="teste">'+data[i].sequencia+'</td>'+ ...
//--------------------------------^ agora class

With this change already the html is right and to get this <td> in click can do so:

$("body").on( "click", ".actions .btn.btn-danger", function() {
    let htmlTeste = $(this).closest("tr").find(".teste").html();
});

Notice that I used % jquery% to use delegation and ensure that on works even in elements that are added to the page in the future.

The click function goes to the nearest closest and <tr> descends again to find with class <td> .

    
26.10.2017 / 11:51