Basic javascript user input modifying div

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>
+4
source share
5 answers

You don't need a loop, this should do it.

function changeWidth(){
    var widthSize=document.getElementById("num").value;
    if(widthSize < 0 || widthSize > 800){
        alert("Not a valid width size"); }
    else {
        document.getElementById("box-1").style.width=widthSize+"px";
    }
}
+3
source

If you are trying to change the width several times, you just need to remove the quotes for your s variable.

Like this:

document.getElementById("box-1").style.width=s;

But if you try to just set the widthSize value and only once, perhaps this will be the following:

document.getElementById("box-1").style.width=widthSize;
return;
+2
source

...

function changeWidth() {
  var widthSize = parseInt(document.getElementById("num").value);
  if (widthSize < 0 || widthSize > 800) {
    alert("Not a valid width size");
  } else {
    document.getElementById("box-1").style.width = widthSize + 'px';
  }
}
.box {
  border: 1px solid black;
  width: 200px;
  height: 200px;
  float: left;
  margin-left: 30px;
  transition: width 1s ease;
}
<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 class="box-1-words" id="b1-words"> Hello World </div>
</div>
+1

s will never be less than 0, since this is the minimum value for your for loop, and if the input number u is less than 800, then the IF condition will never be met. Are you entering a number greater than 800 and still a warning does not appear?

0
source

function changeWidth(){   

var widthSize=document.getElementById("num").value;
            for(var s=0;s<widthSize;s++){
                if(widthSize<0 || widthSize>800){
                    alert("Not a valid width size");
                break;
                }
                else{
                  var a ="transition:2s ease-in;width:"+widthSize+"px;"
                   document.getElementById("box-1").style.cssText=a;
                  
                }
            }
        
		}
.box{border:1px solid black; width:200px; height:200px; float:left; margin-left:30px;}
<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>  </body>
Run code
0
source

Source: https://habr.com/ru/post/1654765/


All Articles