javascript - How correctly check if input is not equal zero -
i have simple code, in input user inputs number , must print numbers until input not equal zero.
and problem when submit value, page stops responding
here how code looks like:
window.onload = function() { var btn = document.getelementsbyclassname('btn')[0]; function printinput() { var output = document.getelementsbyclassname('output')[0]; var input = document.getelementsbyclassname('input')[0].value; while(input !== 0) { var input = document.getelementsbyclassname('input')[0].value; output.innerhtml += input+'<br>'; } } btn.addeventlistener('click', printinput); }
<input type="text" class="input" maxlength="1"> <button class="btn">submit</button> <div class="output"></div>
the value property of input string.
you must compare correct type:
while (input !== '0')
or
while (input != 0)
----- edit -----
consider changing while
if
, otherwise print number different of 0 indefinitely.
window.onload = function() { var btn = document.getelementsbyclassname('btn')[0]; function printinput() { var output = document.getelementsbyclassname('output')[0]; var input = document.getelementsbyclassname('input')[0].value; if(input !== '0') { var input = document.getelementsbyclassname('input')[0].value; output.innerhtml += input+'<br>'; } } btn.addeventlistener('click', printinput); }
<input type="text" class="input" maxlength="1"> <button class="btn">submit</button> <div class="output"></div>
Comments
Post a Comment