Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
-2
rated 0 times [  2] [ 4]  / answers: 1 / hits: 89701  / 14 Years ago, sat, january 8, 2011, 12:00:00

I am having a table with its table row <tr> generating in a loop to form multiple rows.



I want to give separate <a> link to each <tr>. Since in table we can add only add data to <td> only, I am not able to achieve that.



Is there any other way to achieve this?


More From » php

 Answers
4

Html:



<table>
<tr href=http://myspace.com>
<td>MySpace</td>
</tr>
<tr href=http://apple.com>
<td>Apple</td>
</tr>
<tr href=http://google.com>
<td>Google</td>
</tr>
</table>


JavaScript using jQuery Library:



$(document).ready(function(){
$('table tr').click(function(){
window.location = $(this).attr('href');
return false;
});
});


You can try this here: http://jsbin.com/ikada3



CSS (optional):



table tr {
cursor: pointer;
}


OR the HTML valid version with data-href instead of href:



<table>
<tr data-href=http://myspace.com>
<td>MySpace</td>
</tr>
<tr data-href=http://apple.com>
<td>Apple</td>
</tr>
<tr data-href=http://google.com>
<td>Google</td>
</tr>
</table>


JS:



$(document).ready(function(){
$('table tr').click(function(){
window.location = $(this).data('href');
return false;
});
});


CSS:



table tr[data-href] {
cursor: pointer;
}

[#94323] Thursday, January 6, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
havenkamis

Total Points: 172
Total Questions: 93
Total Answers: 103

Location: Wales
Member since Mon, May 17, 2021
3 Years ago
;