Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
<input type="radio" name="Sample1" id="Sample1" value="1" />
<input type="radio" name="Sample1" id="Sample2" value="1" />
<input type="radio" name="Sample2" id="Sample1" value="1" />
<input type="radio" name="Sample2" id="Sample2" value="1" />
etc....
</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?
–
–
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>
<input type="radio" name="Sample1" class="Sample" value="1" />
<input type="radio" name="Sample1" class="Sample" value="1" />
<input type="radio" name="Sample2" class="Sample1" value="1" />
<input type="radio" name="Sample2" class="Sample2" value="1" />
</table>
made some changes here is a working demo
–
–
–
–
–
.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:
<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.
Actually .attr('checked', 'checked')
is more portable.
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.