Michael lacefield Michael lacefield - 1 year ago 113
HTML Question

For loop with user input javascript

So basically I have a for loop and I am trying to get it to run x amount of times. Depending on what the user inputs. The issue I am having is how to get the user input and also make sure that its a number not any other type of input. making them try again if its wrong.

Answer Source

It's simple really

Input Number : <input id="numberinput" onkeypress="return event.charCode >= 48 && event.charCode <= 57" />

<button id="btn" onclick="doAction()">

var doAction = function () {
    var input = document.getElementById('numberinput');
    var times = parseint(input.value);

    for(var i = 0; i < times; i++) {
      //do whatever you need to do
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download