I am trying to make whole row (tr) clickable in table. Here is the code I have tired,
JavaScript
x
20
20
1
<table class="container">
2
<thead>
3
<tr>
4
<th><h1>Id</h1></th>
5
<th><h1>Number</h1></th>
6
<th><h1>Type</h1></th>
7
<th><h1>Value</h1></th>
8
</tr>
9
</thead>
10
<tbody>
11
@for ($i = 0; $i < $count; $i++)
12
<tr class="table-tr" data-url="http://www.engineering.us/gena/details.php">
13
<td>{{ $data_array[$i]['id'] }}</td>
14
<td>{{ $data_array[$i]['number'] }}</td>
15
<td>{{ $data_array[$i]['name'] }}</td>
16
<td>{{ $data_array[$i]['value'] }}</td>
17
</tr>
18
@endfor
19
</tbody>
20
`
And the JS Script,
JavaScript
1
8
1
<script type="text/javascript">
2
$(function () {
3
$(".container").on("click", "tr[data-url]", function () {
4
window.location = $(this).data("url");
5
});
6
});
7
</script>
8
It is not working. Please tell how can we do this.
Thanks in advance 🙂
Advertisement
Answer
JavaScript
1
6
1
$(function() {
2
$('table.container').on("click", "tr.table-tr", function() {
3
window.location = $(this).data("url");
4
//alert($(this).data("url"));
5
});
6
});
JavaScript
1
23
23
1
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
2
<table class="container">
3
<thead>
4
<tr>
5
<th>
6
<h1>Id</h1></th>
7
<th>
8
<h1>Number</h1></th>
9
<th>
10
<h1>Type</h1></th>
11
<th>
12
<h1>Value</h1></th>
13
</tr>
14
</thead>
15
<tbody>
16
<tr class="table-tr" data-url="http://www.engineering.us/gena/details.php">
17
<td>id</td>
18
<td>number</td>
19
<td>manash</td>
20
<td>28</td>
21
</tr>
22
</tbody>
23
</table>