Michael lacefield Michael lacefield - 4 months ago 8
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

It's simple really

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

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

<script>
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
    }
}
</script>