Soy muy nuevo en html y javascript.¿Cómo obtener html <td> valores usando javascript?
Quiero obtener el contenido del elemento siempre que el usuario haga clic en una fila de la tabla mediante javascript.
test.html
<html>
<head>
<script text="text/javascript">
function dispTblContents() {
var pName = document.getElementById("pName").value;
var pAddress = document.getElementById("pAddress").value;
var pEmail = document.getElementById("pEmail").value;
alert(pName + " " + pAddress + " " + pEmail);
}
</script>
</head>
<body>
<table>
<thead>
<tr>
<th>Name</th>
<th>Address </th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr onclick="dispTblContents();" >
<td id="pName">Ricardo Lucero</td>
<td id="pAddress">Mexico City, Mexico</td>
<td id="pEmail">[email protected]</td>
</tr>
</tbody>
</table>
</body>
</html>
Cada vez que hago clic en la fila que muestra undefined undefined undefined
. Sé que mi código es incorrecto, pero realmente no sé cómo solucionarlo. ¿Puede alguien por favor ayudarme? Soy muy nuevo en esto. Gracias por adelantado.
Usar * innerText * o * textContent * (según corresponda) sería mejor para que no se devuelva el marcado. – RobG