I recently visited a website and was checking through its html code and found that two different inputs of type radio were given same id, how can it be possible? I tried to check the radio buttons but I was only able to check the first button how do I check the other buttons using javascript if both are given same id image radio buttons on the website
-
You can't access multiple elements with the same ID from javascript. The developer of the website has made an error. – ADyson Apr 09 '22 at 13:33
-
2both have different values so you could use that as the selector – Lawrence Cherone Apr 09 '22 at 13:34
-
As per the [ask] guide (which you are encouraged to read before posting) please do not include images of code. Code is text and should be provided as text, it's much easier that way for everyone trying to help with your question. And the screenshot really has no bearing at all on the question in this case. You can [edit] the question to correct it. – ADyson Apr 09 '22 at 13:42
-
What about if you try to get all the input fields with name and then try to select specific one with the index? – Muhammad Umair Apr 09 '22 at 13:39
1 Answers
From MDN
elements of type radio are generally used in radio groups—collections of radio buttons describing a set of related options.
Only one radio button in a given group can be selected at the same time. Radio buttons are typically rendered as small circles, which are filled or highlighted when selected.
Every element on your document is expected to have a unique id
value for its id
property otherwise odd things would happen when trying to select those elements using a css selector both in styles and js (to cite one).
Here there's an in depth discussion on SO for example for further reference.
But for sure they are meant to be grouped using the same name
attribute value to hint which group is expected to have a mutually exclusive checked status and to converge to the same GET/POST parameter, named like that name
property and having as value the value of the only radio selected in its group, when its form will be submitted.

- 6,156
- 2
- 17
- 30