I am trying to change the width of a field using user input using a for and if-else loop. It seems that Cant did not execute the first if statement. Here is the javascript:
function changeWidth(){
var widthSize = document.getElementById("num").value;
for(var s = 0; s < widthSize; s++){
if(s < 0 || s > 800){
alert("Not a valid width size"); }
else {
document.getElementById("box-1").style.width="s";
}
}
}
CSS:
.box {
border: 1px solid black;
width: 200px;
height: 200px;
float:left;
margin-left:30px;
}
HTML:
<input type="number" id="num" name="num" value="" placeholder="Please enter width size" />
<input type="button" id="btt3" name="btt3" value="Generate size" onclick="changeWidth();" />
<div class="box" id="box-1"></div>
source
share