getElementByName returns Type Error?

jth41 picture jth41 · Apr 14, 2013 · Viewed 13k times · Source

My code:

var isSomethingChecked = (document.getElementByName("koalaCheck").checked ||
                          document.getElementByName("kangarooCheck").checked);

Why does this code throw an exception called "Type Error"?

Answer

DiverseAndRemote.com picture DiverseAndRemote.com · Apr 14, 2013

There is no function called getElementByName. what you need is getElementsByName which returns an array of all of the elements that have that name. so you can use:

var isSomethingChecked = (document.getElementsByName("koalaCheck")[0].checked ||    
                         document.getElementsByName("kangarooCheck")[0].checked);