相关文章推荐
帅气的洋葱  ·  VSCode报错 Unexpected ...·  1 月前    · 
腼腆的机器猫  ·  C# ...·  3 月前    · 
强悍的足球  ·  c# 与java base64 ...·  7 月前    · 
善良的海豚  ·  Fabric.js 清空画布 - ...·  1 年前    · 
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?

Indeed. IDs are unique identifiers for elements. What on earth are you trying to accomplish here? – Lightness Races in Orbit Apr 12, 2011 at 14:07 @TheAdamGaskins you need to tell your boss then that his inputs are invalid and he needs to change them you can't just ignore the rules of code without consequence – mcgrailm Apr 12, 2011 at 14:20

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

This works perfectly, although with the ID's it only ever selects the first radio button (which is expected). I'll have to talk to the boss about switching to class... – Entity Apr 12, 2011 at 14:21 @theadamgaskins please see my note above about the id's its really not optional you simply can't have elements with the same id or you will have bad behavior – mcgrailm Apr 12, 2011 at 14:23 "elements can only have one id" is technically speaking incorrect however, it MUST be unique in the elements subtree. – Mark Schultheiss Oct 24, 2017 at 12:57 Oh, you are splitting hairs here, @MarkSchultheiss I think people understand what is meant. However if you would like to edit my post be my guest. – mcgrailm Oct 24, 2017 at 19:07 No worries, that actually kind of goes away with more recent HTML specs also. Comment really was on my part intended for others as I see a lot of "broken" html with duplicate id's etc (not by you!) i actually upvoted – Mark Schultheiss Oct 24, 2017 at 19:24

.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.

Yeah... I'm not really in control of that. My Boss gave me a URL to a page that generates tables in this format... so thats what I have to work with. – Entity Apr 12, 2011 at 14:14

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.