Reputation: 3760
I have radio buttons in table like this
<form>
<table class="table table-hover flights_table">
<tr>
<td>
<input type="radio" class="flight" name="flight" />
</td>
<td>Fare type</td>
<td>Price</td>
<td>dep</td>
<td>Arrival</td>
</tr>
<tr>
<td>
<input type="radio" class="flight" name="flight" />
</td>
<td>Fare type</td>
<td>Price</td>
<td>dep</td>
<td>Arrival</td>
</tr>
<tr>
<td>
<input type="radio" class="flight" name="flight" />
</td>
<td>Fare type</td>
<td>Price</td>
<td>dep</td>
<td>Arrival</td>
</tr>
</table>
</form>
and jQuery code
$('.flights_table tr').click(function (e) {
//$('.flight').attr('checked', false);
$(this).find('.flight').attr('checked', true);
});
the aim is to select a radio when clicked on row in which it is, and it works but only once for each radio.
e.g You select 1st then You click 2nd and If You try to select 1st again it doesn't work.
Solution I used was accepted as working in this question but doesn't work perfectly any more.
I tried with removeAttr as well in place of commented line same result.
Upvotes: 1
Views: 901
Reputation:
Try this
$('tr').click(function (e) {
$(this).$(input:radio).prop('checked', true);
});
Upvotes: 2
Reputation: 14827
The problem is the incompatible issue of attr()
with jQuery version 1.9+.
As of jQuery 1.6, the .attr() method returns undefined for attributes that have not been set. To retrieve and change DOM properties such as the checked, selected, or disabled state of form elements, use the .prop() method.
Try using prop instead:
$('.flights_table tr').click(function (e) {
//$('.flight').attr('checked', false);
$(this).find('.flight').prop('checked', true);
});
Upvotes: 1