Skip to content Skip to sidebar Skip to footer

How Do I Make A Button Only Available During A Certain Time Of Day In Html?

Solution 1:

Couple things wrong: 1) if you want it to be available from 6-8 your if statement should look like if (h >= 6 && h <= 8) and 2) to remove the disabled attribute remove the parenthesis and .style so that you have .disabled = false.

HTML:

  <input id="bttHider" class="bttHider" type="submit" onclick="showHidden(); showbttHidden();" value="Add a new notice here." disabled>

JS:

  var h = new Date().getHours();

  if (h >= 6 && h <= 8) {
        document.getElementById('bttHider').disabled=false;
  } 

Fiddle:

http://jsfiddle.net/8tf46zqu/3/


Solution 2:

Try something like this, without declaring datatype and with proper date object and your script within the body tag... Also, instead on piting onclick in your html, use an event listner in your javascript. If you read uo a little on event listners and javascript in general you will be writing a much better javascript...

<body>
    <input id="bttHider" class="bttHider" type="submit"onclick="showHidden(); showbttHidden();" value="Add a new
notice here.">

<script>

    var h = new Date().getHours(); 
    var butn = document.getElementById('btttHider');
    if (h <= 5 && h >=7) ){
        btn.disabled=false;
    } else { 
     btn.disabled=true;
}
</script>
</body>

Post a Comment for "How Do I Make A Button Only Available During A Certain Time Of Day In Html?"