Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
5
rated 0 times [  10] [ 5]  / answers: 1 / hits: 64208  / 13 Years ago, thu, october 20, 2011, 12:00:00

Im very new to html and javascript.



I want to get the content of element whenever the user click on a table row using 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>


Whenever I click the row it displays undefined undefined undefined. I know my code is wrong but I really don't how to fix this. Can somebody please help me. Im very new to this thing. Thanks in advance.


More From » html

 Answers
39

You need innerHTML not value here, value is used for form elements.



<script text=text/javascript>
function dispTblContents() {
var pName = document.getElementById(pName).innerHTML;
var pAddress = document.getElementById(pAddress).innerHTML;
var pEmail = document.getElementById(pEmail).innerHTML;

alert(pName + + pAddress + + pEmail);
}
</script>


You might also want to look into jQuery if you're not using it yet, it makes selecting and manipulating HTML with Javascript a lot easier.


[#89524] Tuesday, October 18, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
desiraeleandrah

Total Points: 202
Total Questions: 111
Total Answers: 115

Location: Macau
Member since Mon, Nov 16, 2020
4 Years ago
desiraeleandrah questions
Thu, Nov 19, 20, 00:00, 4 Years ago
Wed, Nov 11, 20, 00:00, 4 Years ago
Wed, Oct 14, 20, 00:00, 4 Years ago
;