How do I show a row in fields of the form?

0

I have a table which is generated from the database with PHP so I do not assign a id , the idea is to show the client records to assign them an address (with another form but I need the id of the client), I have functionality but reloading the page what I want is to do it with JavaScript and that simply when I give it in a link that is in the table (in the row of each client) that shows them in the fields of the forms without reload the page, but I have two days of learning it just do not know how to implement it. This is the html table already generated:

<table class="table table-bordered" id="tblClientes">
    <thead>
        <tr>
            <th>ID Cliente</th>
            <th>Cliente</th>
            <th>Documento</th>
        </tr>
    </thead>
    <tbody>
        <tr>            
            <td>1</td>
            <td>Cliente1 apellido1</td>
            <td>5467915414</td>
            <td>
                <center>
                    <a onclick="alertar(this)" class="waves-effect waves-dark" href="#" aria-expanded="false">
                        <i class="fa fa-edit" data-toggle="tooltip" title="" data-original-title="Seleccionar"></i>
                        <span class="hide-menu"></span>
                    </a>
                </center>
            </td>
        </tr>
        <tr>            
            <td>2</td>
            <td>Cliente2 apellido2</td>
            <td>5467915414</td>
            <td>
                <center>
                    <a onclick="alertar(this)" class="waves-effect waves-dark" href="#" aria-expanded="false">
                        <i class="fa fa-edit" data-toggle="tooltip" title="" data-original-title="Seleccionar"></i>
                        <span class="hide-menu"></span>
                    </a>
                </center>
            </td>
        </tr>
    </tbody>
</table>

And here I would send the data you select from the table (part of the form)

<div class="column">
    <label for="cliente">Cliente:</label>
    <input readonly style="width:88%;" type="text" class="form-control" id="cliente"  name="cliente">
</div>
    <div class="column">
        <label for="doc">Identificación:</label>
        <input readonly type="text" class="form-control" id="doc" name="doc">
</div>

This is javascript

function alertar(elemento){
    try{
        //trato de obtener el padre del a que seria el que llama a la funcion
        //pero me lo tira como undefined y entonces no puedo acceder al padre de este
        //del center para llegar hasta el <tr> para leer la fila
        var centerEl = elemento.parentNode;
        alert(centerEl.constructor);
    }catch(err){
        alert(err.message);
    }
    alert("Hola de prueba a la funcion");
}

What I try to do is to get to the <tr> with the <a> element that executes the function since there is no id (as the table comes from the base), I have a base of how to read the row of the table the problem would be that I do not get to get the row to do it since it tells me that it is undefined .

    
asked by Dev. Joel 11.02.2018 в 19:47
source

1 answer

0

If you want to access the tr you must do 3 times the parentNode , the event is given in the a tag, when you make the first parentNode you will choose the center tag if you get this the parent node (parentNode) will get the td and if the node is also selected again, you will get the tr , to get the values of td, we would search for the td tag with getElementsByTagName

function alertar(elemento){
   var tr = elemento.parentNode.parentNode.parentNode;
   var columns = tr.getElementsByTagName('td');
   console.log(columns[1].innerText);//segunda columna
}
<table class="table table-bordered" id="tblClientes">
    <thead>
        <tr>
            <th>ID Cliente</th>
            <th>Cliente</th>
            <th>Documento</th>
        </tr>
    </thead>
    <tbody>
        <tr>            
            <td>1</td>
            <td>Cliente1 apellido1</td>
            <td>5467915414</td>
            <td>
                <center>
                    <a onclick="alertar(this)" class="waves-effect waves-dark" href="#" aria-expanded="false">Mostrar
                        <i class="fa fa-edit" data-toggle="tooltip" title="" data-original-title="Seleccionar"></i>
                        <span class="hide-menu"></span>
                    </a>
                </center>
            </td>
        </tr>
        <tr>            
            <td>2</td>
            <td>Cliente2 apellido2</td>
            <td>5467915414</td>
            <td>
                <center>
                    <a onclick="alertar(this)" class="waves-effect waves-dark" href="#" aria-expanded="false">Mostrar
                        <i class="fa fa-edit" data-toggle="tooltip" title="" data-original-title="Seleccionar"></i>
                        <span class="hide-menu"></span>
                    </a>
                </center>
            </td>
        </tr>
    </tbody>
</table>
    
answered by 11.02.2018 / 20:16
source