Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getElementByName returns Type Error?

My code:

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

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

like image 711
jth41 Avatar asked Apr 14 '13 02:04

jth41


People also ask

What does getElementsByName return?

getElementsByName() The getElementsByName() method of the Document object returns a NodeList Collection of elements with a given name attribute in the document.

How do you get getElementsByName value?

How it works: First, select the submit button by its id btnRate using the getElementById() method. Second, listen to the click event of the submit button. Third, get all the radio buttons using the getElementsByName() and show the selected value in the output element.

How do I find the input name of an element?

The getElementsByName() method returns a collection of elements with a specified name. The getElementsByName() method returns a live NodeList.


2 Answers

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);
like image 162
DiverseAndRemote.com Avatar answered Oct 25 '22 06:10

DiverseAndRemote.com


That would be because the correct method is document.getElementsByName(). You missed an s.

View the documentation.

Assuming you do not wish to check each checked state per element(as this method returns an array).. I would use document.getElementById().. but that is without seeing your html.

like image 28
Daedalus Avatar answered Oct 25 '22 07:10

Daedalus