Create a link that when clicked I get your text

3

I have a table that has the Material ID and the Name, the name is a link, I need to make it when I click the link it takes the ID and the name puts it in a text field in the form.

The table is generated dynamically by a script

$('#tbl').append("<tr class=\"corpoTbl\"> <td class=\"ids\">" + item.ID +
                        "</td><td class=\"nome\"><a href=\"#\" onclick=\"Seleciona()\">" + item.Nome + "<\a></td></tr>")

I'm trying to get the link text like this:

function Seleciona(event) {
    var id = event.ID
    var nome = event.Nome

    $('#ID').val(ID);
    $('#nome').val(nome);

    $(this).dialog("close");
}
    
asked by anonymous 21.05.2015 / 15:58

2 answers

2

My suggestion is you already preload your link passing the values to the Seleciona method.

See Working:

   $(function () {
            $('#tbl').append("<tr class=\"corpoTbl\"> <td class=\"ids\">" + 1 +
            "</td><td class=\"nome\"><a href=\"#\" onclick=\"Seleciona('"+1+"',"+ "'Teste')\">" + "Teste" + "<\a></td></tr>")

        });
        function Seleciona(id, nome) {
            $('#ID').val(id);
            $('#nome').val(nome);
        }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><tableid="tbl">
    </table>
    <input type="text" id="ID" />
    <input type="text" id="nome" />
    
21.05.2015 / 16:15
3

In the construction of your table you send your item.ID main that will define each td (also had errors in the tags):

$('#tbl').append("<tr class=\"corpoTbl\"> <td class=\"ids\" id=\"tdID_" + item.ID +">" + item.ID +
                        "</td><td class=\"nome\" id=\"tdNome_" + item.ID +"><a href=\"#\" onclick=\"Seleciona('"+ item.ID +"')\">" + item.Nome + "</a></td></tr>")

In Seleciona you get a idlinha that corresponds to your item.ID , so you can try to get the id and nome data:

function Seleciona (idlinha) {
    var id = $("#tdID_" + idlinha).html();
    var nome = $("#tdNome_"+idlinha+" a").text();   

    $('#inp_ID').val(id);
    $('#inp_Nome').val(nome);

    $(this).dialog("close");
}

Functional example:

Seleciona = function (idlinha) {
    var id = $("#tdID_" + idlinha).html();
    var nome = $("#tdNome_"+idlinha+" a").text();   

    $('#inp_ID').val(id);
    $('#inp_Nome').val(nome);

    $(this).dialog("close");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><tableborder="1">
    <tr class="corpoTbl"> 
        <td class="ids" id="tdID_12">12</td>
        <td class="nome" id="tdNome_12">
            <a href="#" onclick="Seleciona(12)">Fernando Trambolho</a>
        </td>
    </tr>
 </table>
<br/>
<input type="text" id="inp_ID"/>
<input type="text" id="inp_Nome"/>
    
21.05.2015 / 16:19