Search code examples
javascripthtmlformsif-statementverification

JavaScript if and else if statement not working properly


I have an input type='number' and a button to indicate function testfun() and an if statement in the function that is supposed to validate that the number is within range and provide clear instructions.

when I enter a number within the range of 1-1000 it works fine.

when I enter a number that is <1 or >1000 the <div><p> does not populate.

HTML:

<div class="preT" id="preT">
  <form>
    <label for="voipLines">VoIP Lines</label>
    <input type="number" id="voipLines" name="voipLines" requiered min="1" max="1000" value="1">
  </form>
  <div class="voipbutton" id="voipbutton">
    <button class="button" onclick="testfun()">Test Clicker</button>
  </div>
</div>
<div id="duringT">
  <p Id="clickerTest"></p>
  <div Id="prBar" data-label=""></div>
  <div id="canvascon"></div>
</div>

JS:

function testfun() { //used in HTML
  var voip = document.getElementById("voipLines").value;
  if (voip < 1) { //voiplines verification 
    return document.getElementById("clickerTest").innerHTML = "<div><p>Make Sure VoIP Lines is within the range of 1 to 1000</p></div>";
  } else if (voip > 1000) {
    return document.getElementById("clickerTest").innerHTML = "<div><p>Make Sure VoIP Lines is within the range of 1 to 1000</p></div>";
  } else {
    var voipGuide = document.getElementById("clickerTest").innerHTML = '<h2>Clicker Successful!!!</h2>\
    <p>Please click the link below to start <b>' + voip + ' test</b></p>\
    <p>To change the amount of tests to run <a href= "">Click Here</a></p>';
  document.getElementById('preT').style.display = 'none';
    document.getElementById("canvascon").innerHTML = '<div id="canvas-container"><canvas id="canvs3" style="background-color:#F0F0F0; ">Your browser does not support the HTML5 canvas tag.</canvas><br><br></div>';
    voipsims();
    bootfun();
  }
}

Solution

  • There was a typo: the property is .innerHTML, not .innerhtml. Working example:

    function testfun() { //used in HTML
      var voip = document.getElementById("voipLines").value,
          clickerTest = document.getElementById("clickerTest");
            
      if (voip < 1) //voiplines verification 
        return clickerTest.innerHTML = "<div><p>Make Sure VoIP Lines is within the range of 1 to 1000</p></div>";
      
      else if (voip > 1000)
        return clickerTest.innerHTML = "<div><p>Make Sure VoIP Lines is within the range of 1 to 1000</p></div>"; 
      
      else {
        
        var voipGuide = clickerTest.innerHTML = '<h2>Clicker Successful!!!</h2>\
        <p>Please click the link below to start <b>' + voip + ' test</b></p>\
        <p>To change the amount of tests to run <a href= '
        '>Click Here</a></p>';
        document.getElementById('preT').style.display = 'none';
        document.getElementById("canvascon").innerHTML = '<div id="canvas-container"><canvas id="canvs3" style="background-color:#F0F0F0; ">Your browser does not support the HTML5 canvas tag.</canvas><br><br></div>';
        
      }
    }
    <div class="preT" id="preT">
      <form>
        <label for="voipLines">VoIP Lines</label>
        <input type="number" id="voipLines" name="voipLines" min="1" max="1000" value="1" required>
      </form>
      <div class="voipbutton" id="voipbutton">
        <button class="button" onclick="testfun()">Test Clicker</button>
      </div>
    </div>
    <div id="duringT">
      <p id="clickerTest"></p>
      <div id="prBar" data-label=""></div>
      <div id="canvascon"></div>
    </div>