0

I'm making an age checker just for fun and I'm having trouble with this. I want a code that creates following:

<form action="proceed.html">
    <input type="submit" value="Proceed" />
</form>

in a condition. If the age >= 13, create the button. This is what I have so far.

function agechecker() {

  var uiage = document.getElementById("uiage").value;
  var uiageop = "";

  if (uiage > 0 && uiage < 115) {
    if (uiage >= 13) {
      uiageop = "You have successfully met/surpassed the age requirements!"
      <!--Add button here-->
    } else {
      uiageop = "You are too young to meet/surpass the age requirements."
    }
  } else {
    uiageop = "Please enter a valid age."
  }
  document.getElementById("ageverifyop").innerHTML = uiageop
}
<div class="agechecker">
  <h1>Check your age!</h1>
  <label>Your age: <input type="number" placeholder="Enter age" id="uiage"></label>
  <label><input type="submit" onclick="return agechecker()"></label>
  <h4 id="ageverifyop"></h4>
</div>
Yash
  • 11,486
  • 4
  • 19
  • 35
Maanush27
  • 1
  • 3

3 Answers3

0

Just simply add a button on html and show and hide it according to the age inserted by the user.

In JQuery:

$("#yourButton").show();
$("#yourButton").hide();
0

Better use jQuery, it is much more easier. But if you wanna use plain javascript, you can create an element by setting to visibility:hidden. Then, you can show and hide depending on condition.

function agechecker() {

  var uiage = document.getElementById("uiage").value;
  var uiageop = "";

  if (uiage > 0 && uiage < 115) {
    if (uiage >= 13) {
      uiageop = "You have successfully met/surpassed the age requirements!"
      <!--Add button here-->
      document.getElementById("proceed").style.visibility = "visible";
    } else {
      uiageop = "You are too young to meet/surpass the age requirements."
      document.getElementById("proceed").style.visibility = "hidden";
    }
  } else {
    uiageop = "Please enter a valid age."
    document.getElementById("proceed").style.visibility = "hidden";
  }
  document.getElementById("ageverifyop").innerHTML = uiageop
}
<div class="agechecker">
  <h1>Check your age!</h1>
  <label>Your age: <input type="number" placeholder="Enter age" id="uiage"></label>
  <label><input type="submit" onclick="return agechecker()"></label>
  <h4 id="ageverifyop"></h4>
  <div id="proceed" style="visibility: hidden">
      <form action="proceed.html">
        <input type="submit" value="Proceed" />
      </form>
  </div>
</div>
kzharas210
  • 440
  • 1
  • 5
  • 13
  • This is working, but it only changes the visibility and I can click it when it's invisible. Is there a way to remove/toggle the button similarly? – Maanush27 Nov 10 '17 at 21:08
0

If you want to use jQuery, you may try this:

$(document).ready(function(){
  
  $('#uiage').on('change input', function(){
    if($(this).val().length < 1){
      $("#ageverifyop").html("");
      //$('input[type="submit"]').css('display', 'none');
    }else if($(this).val() < 13){
      $('input[type="submit"]').css('display', 'none');
      $("#ageverifyop").html("You're too young.");
    } else if($(this).val() >= 13){
      $('input[type="submit"]').css('display', 'inline-block');
      $("#ageverifyop").html("Your age is valid.");
    }
  });
  
});
input[type="submit"]{
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<div class="agechecker">
  <h1>Check your age!</h1>
  <label>Your age: <input type="number" placeholder="Enter age" id="uiage"></label>
  <label><input type="submit"></label>
  <h4 id="ageverifyop"></h4>
</div>
Blues Clues
  • 1,694
  • 3
  • 31
  • 72