My code:
var isSomethingChecked = (document.getElementByName("koalaCheck").checked ||
document.getElementByName("kangarooCheck").checked);
Why does this code throw an exception called "Type Error"?
getElementsByName() The getElementsByName() method of the Document object returns a NodeList Collection of elements with a given name attribute in the document.
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.
The getElementsByName() method returns a collection of elements with a specified name. The getElementsByName() method returns a live NodeList.
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);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With