Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript NaN error in Internet Explorer

I have a form with radio buttons:

<ul class='likert'>
  <li>
    <input type="radio" name="q1" value=5 required>
  </li>
  <li>
    <input type="radio" name="q1" value=4>
  </li>
  <li>
    <input type="radio" name="q1" value=3 checked="checked">
  </li>
  <li>
    <input type="radio" name="q1" value=2>
  </li>
  <li>
    <input type="radio" name="q1" value=1>
  </li>
</ul>

In Firefox and Chrome,

parseInt(document.forms["question_form"]["q1"].value)

returns 3 (or whatever) but in Internet Explorer I get ‘NaN’.

like image 328
user1234 Avatar asked Jun 16 '16 15:06

user1234


People also ask

Why am I getting NaN in JavaScript?

In JavaScript, NaN stands for Not a Number. It represents a value which is not a valid number. It can be used to check whether a number entered is a valid number or not a number.

What is JavaScript NaN?

In JavaScript, NaN is short for "Not-a-Number". In JavaScript, NaN is a number that is not a legal number. The Global NaN property is the same as the Number.

What is NaN error?

NaN is an error value that means not a number. However, JavaScript considers the type of NaN to be number. Infinity is a value so big it can't be represented by JavaScript numbers.


1 Answers

IE returns a HTMLCollection when the names are not unique in document.forms["question_form"]["q1"].

See Remarks at IHTMLElementCollection

"If duplicate names are found, a collection of those named items is returned."

You can get the checked value for example like this:

var val = document.querySelector('.likert input:checked').value);
like image 161
Teemu Avatar answered Oct 11 '22 15:10

Teemu