简体   繁体   中英

How can I get the value from a cell which is in the same row as the button I am pressing

So I'm trying to get the value of the USUARIO column when I'm pressing the button of its same row: For example, if I was about to press the first button, it should give me 3 as a result

In my code I have this:

function(response) {
                        var t = "";
                        var tr = "";
                        tr += "<thead>";
                        tr += "<th>USUARIO</th>";
                        tr += "<th>FECHA INICIO</th>";
                        tr += "<th>FECHA FIN</th>";
                        tr += "<th>TIPO EMERGENCIA</th>";
                        tr += "<th>LOCALIZACIÓN</th>";
                        tr += "<th>DESACTIVADOR</th>";
                        tr += "</thead>";
                        tr +=  '<tbody id="tbody' +  i +">';
                        tr += '<td class= "OIDCELL">' + response[i].usuario_oid + "</td>";
                        tr += "<td>" + response[i].fechainicio + "</td>";
                        tr += "<td>" + response[i].fechafin + "</td>";
                        tr += "<td>" + response[i].tipoemergencia_tipo
                                + "</td>";
                        tr += "<td>" + enlace + "</td>";
                        if (response[i].desactivador == 0){
                            tr += '<td> <button type="button" onclick="cambiarDesactivador()">Desactivar</button></td>';
                        }else{
                            tr += "<td>" + response[i].desactivador + "</td>";
                        }
                        
                        tr += "</tr>";
                        tr += "</tbody>";
                        }
                        t += tr;
                        document.getElementById("historicoUser").innerHTML += t;
                    }

So in which way am I able to get the value of the USUARIO column of the same row of the pressed button?

Thank you in advance!

You could directly pass in the value response[i].usuario_oid which you are showing in the column to your button's onClick function

example: cambiarDesactivador(response[i].usuario_oid)

Some comments on the code, which has many errors:

  1. function(response) {...} is not a valid function declaration or expression
  2. The t variable seems redundant
  3. Don't mix the semantics of double and single quotes. Use single quotes for javascript, double for HTML (see below)
  4. All that += is inefficient, just create a single string and assign it to tr like:
tr = '<thead>' +
     '<th>USUARIO</th>' +
     '<th>FECHA INICIO</th>' +
     ...
     '<tbody id="tbody' +  i + '">' +
     ...

To get the row of the button that is clicked, get a reference to the button then use closest to get the tr ancestor. Table rows have a cells collection, the first cell is index 0, etc.

So pass this from the listener, go up to the row then get the content of the first cell, eg

 function getFirstCellContent(el) { let row = el.closest('tr'); console.log(row.cells[0].textContent); }
 table { border-left: 1px solid #999999; border-top: 1px solid #999999; border-collapse: collapse; } th, td { border-right: 1px solid #999999; border-bottom: 1px solid #999999; }
 <table> <tr><td>One<td><button onclick="getFirstCellContent(this)">Click me</button> <tr><td>Two<td><button onclick="getFirstCellContent(this)">Click me</button> <tr><td>Three<td><button onclick="getFirstCellContent(this)">Click me</button> </table>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM