Reputation: 9
how can i select a radio button with its value is the only one which is unique via JS. This seems to be little odd, but i need to. the Html code is as follows
<td class="Customer_Name"><table>
<td>
<input name="Name" tabindex="4" id="Name" type="radio" value="01">John</td>
<input name="Name" tabindex="4" id="Name" type="radio" value="02">Sam</td>
<input name="Name" tabindex="4" id="Name" type="radio" value="03">Fred</td>
<input name="Name" tabindex="4" id="Name" type="radio" value="04">Peter</td>
<td>
Upvotes: 0
Views: 99
Reputation: 87233
You cannot have same id
for multiple elements. id
should be unique.
You can use querySelector
with attribute-value selector.
document.querySelector('input[type="radio"][value="02"]')
document.querySelector('input[type="radio"][value="02"]').checked = true;
<td class="Customer_Name">
<table>
<td>
<input name="Name" type="radio" value="01" />John</td>
<td>
<input name="Name" type="radio" value="02" />Sam</td>
<td>
<input name="Name" type="radio" value="03" />Fred</td>
<td>
<input name="Name" type="radio" value="04" />Peter</td>
<td>
Using jQuery
$('input[type="radio"][value="02"]').prop('checked', true);
Upvotes: 2