justin
justin

Reputation: 9

Radio Button-selection based on value

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

Answers (1)

Tushar
Tushar

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"]')

Demo

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

Related Questions