Reputation: 8202
I have a table like so:
<table>
<tr>
<td>
<input type="radio" name="Sample1" id="Sample1" value="1" />
<input type="radio" name="Sample1" id="Sample2" value="1" />
</td>
<td>
<input type="radio" name="Sample2" id="Sample1" value="1" />
<input type="radio" name="Sample2" id="Sample2" value="1" />
</td>
etc....
</tr>
</table>
I want to be able to select a specific radio button by name
and id
. E.G., select the radio button with the name Sample2
and id Sample1
. I tried doing this:
$('[id="Sample1"][name="Sample2"]').checked = true;
But no luck... How should I be doing this?
Upvotes: 9
Views: 40329
Reputation: 17640
$('#Sample1[name="Sample2"]').attr('checked','checked');
but elements can only have one id so maybe you want class instead of id
$('.Sample1[name="Sample2"]').attr('checked','checked');
then your html
<table>
<tr>
<td>
<input type="radio" name="Sample1" class="Sample" value="1" />
<input type="radio" name="Sample1" class="Sample" value="1" />
</td>
<td>
<input type="radio" name="Sample2" class="Sample1" value="1" />
<input type="radio" name="Sample2" class="Sample2" value="1" />
</td>
</tr>
</table>
EDIT
made some changes here is a working demo
Upvotes: 12
Reputation: 385108
.checked = true
is wrong. Use .attr('checked', true)
.
Also, you may only use an ID once. IDs are unique identifiers for elements. I have no idea what you're trying to accomplish here, but:
<html>
<body>
<input type="radio" name="Sample1" id="SampleA" />
<input type="radio" name="Sample1" id="SampleB" />
<input type="radio" name="Sample2" id="SampleC" />
<input type="radio" name="Sample2" id="SampleD" />
<script type="text/javascript">
$(function() {
$('#SampleC[name="Sample2"]').attr('checked', true);
// [id="SampleC"][name="Sample2"] works too
});
</script>
</body>
</html>
does the job.
Edit
Actually .attr('checked', 'checked')
is more portable.
Upvotes: 3