Can getElementById retrieve 2 elements with same Id?

ehchn1
ehchn1 used Ask the Experts™
on
Hi,

<input type="radio" name="yesno" value="true">
<input type="radio" name="yesno" value="false">

For the above, are there any ways to access both the radio buttons through getElementById ? I only managed to retrieve the 1st element since "yesno" is referring to the 1st element.

I know that I can do something like:
this.form.yesno[index]
but for some reason, I need to stick with getElementById!

Any ideas?

Thanks in advance.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Yes you can get the objects properties by ,

alert(document.getElementById("idValue").tagName);

You can change the tagName attribute with the attribute u need (like id, name, style etc.,)

Commented:
Try adding an id to the input tags:

<html>
<head>
<title>No title</title>
</head>

<script>
     function init()
     {
          alert(document.getElementById("input1").value);
          alert(document.getElementById("input2").value);
     }
</script>

<body onload="init();">

<form >
<input id="input1" type="radio" name="yesno" value="true">
<input id="input2" type="radio" name="yesno" value="false">
</form>

</body>
</html>


Regards,
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Commented:
why do you have to use document.getElementById?

using document.getElementsByName("yesno") would get you what you want -- in case you were being forced to use only DOM methodology.

getElementsByName returns an array of everything with that name...

I don't think you can get what you want with getElementById unless you follow DTACK's solution and actually give them individual IDs

Commented:
FYI: in the microsoft MSDN documentation for getElementById, "If the document has multiple elements with the matching ID, this method returns the first matching element in the document."

here is the link http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemxmlxmldocumentclassgetelementbyidtopic.asp
Michel PlungjanIT Expert
Top Expert 2009

Commented:
Please finalise this question. I cannot decide who should get the points. Please discuss among yourselves if ehchn1 does not reply.
Thanks

Michel, cleanup volonteer
Michel PlungjanIT Expert
Top Expert 2009

Commented:
ehchn1,
No comment has been added lately (86 days), so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area for this question:

RECOMMENDATION: delete / Point Refund

I find it completely silly to use document.getAnything when you can use
document.forms[0].yesno[0].checked or [1].checked
works in all browsers

Please leave any comments here within 7 days.

-- Please DO NOT accept this comment as an answer ! --

Thanks,

mplungjan
EE Cleanup Volunteer

Commented:
Well i like using the DOM thats the reason...
Kinda to the point, shortens up ur code quite some and isn't more difficult to read, specially if u know what you're doing :-)

The only argument against it would maybe be browser compat. with older browsers...

By the way, the Best Wishes for this New Year!

Manuel
Michel PlungjanIT Expert
Top Expert 2009

Commented:
document.getElementsByName("yesno")[0]
document.forms[0].yesno[0]

which is the shortest? ;)

Commented:
LOL :-p yeah in this case you are right cuz the example is not really complicated,
But if you have more nested html code (like nested div's, blabla, etc...) then using it makes it shorter...

Commented:
in general and for quick scripting I use document.forms["formname"].elements["elementname"] because it is easy to program with... but the other reason that getElementBy is better is that it returns a null if the thing isn't found.
It makes for simpler, and more robust scripting if you are doing more complicated stuff.

<script>
var arrElements = document.getElementsByName("yesno")
if ( arrElements.length )
{
    arrElements[0].checked = true
}
else
{
    alert("error: no element")
}

var objForm = document.forms[0]
if ( typeof objForm != "undefined" )
{
    var arrElements = objForm.yesno
    if ( typeof arrElements != "undefined" )
    {
        arrElements[0].checked = true
    }
    else
    {
        alert("error: no element")
    }
}
else
{
    alert("error: no form")
}
</script>
Michel PlungjanIT Expert
Top Expert 2009

Commented:
Or simpler

var objForm = document.forms[0]
if (objForm) {
    var arrElements = objForm.yesno
    if (arrElements)  arrElements[0].checked = true
    else alert("error: no element")
}
else  alert("error: no form")

Michel
Commented:
PAQed - no points refunded (of 100)

modulo
Community Support Moderator

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial