Jenny Prs
Jenny Prs

Reputation: 43

parseInt returns NaN ( js )

var serviceB = document.getElementById('service').value;

var service = parseInt(serviceB, 10);

function showMessage() {
  alert(service);
  alert(typeof(service)); //returns number
}
<label for="service">Number of services</label>
<input type="text" id="service" name="service" min="1" max="10" required="required">
<input id="btn" type="submit" value="Submit" onclick="showMessage()">

I want to take the number of services to do a FOR loop, and when i test the alert function it returns NaN... I also tried it without the ",10" but it does the same thing. can anyone please help me ?

Upvotes: 0

Views: 121

Answers (1)

Ar Rakin
Ar Rakin

Reputation: 542

Please use <input type="number">. If you type something that is not a number (e. g. 5f), then it will return NaN.

Upvotes: 2

Related Questions