-1

Is there a way to check if the value is above 300 using jQuery? I have made the script below but I have no idea on how to check if the value is above 300.

$(document).ready(function(){
    $("input").on('keyup', function() {
        if( $(this).val() == '300' ) {
             alert("Stop it now! You're above the 300 mark! I told you to stop, you dum dum!");
        }
    });
});
<input type="text">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

Thanks in advance!

Sergey
  • 1,608
  • 1
  • 27
  • 40
Jason Sons
  • 35
  • 5
  • 1
    Instead of checking for equality (==), check for greater than (>): `if( $(this).val() > 300 ) {` – baao Dec 21 '16 at 19:13
  • 1
    `$(this).val()` gets you the value of the input. You're currently checking for equality with 300. If you want to check if it's above, change it to `>`. Also, just FYI, `$("input")` will select every single input, rather than a specific one – Honinbo Shusaku Dec 21 '16 at 19:14
  • related: http://stackoverflow.com/questions/13079626/javascript-if-number-greater-than-number – Kevin B Dec 21 '16 at 19:16
  • https://jsfiddle.net/5azqktqu/ – Kevin B Dec 21 '16 at 19:25

1 Answers1

-1

Parse the value to int.

    $(document).ready(function(){
        $("input").on('keyup', function() {
            if( Number($(this).val()) > 300 ) {
                 alert("Stop it now! You're above the 300 mark, damn it! I told you to stop, you dum dum!");
            } else {
               //Hide your div here
               $("#ElementID").hide();
            }
        });
    });
Kevin B
  • 94,570
  • 16
  • 163
  • 180
DonO
  • 1,030
  • 1
  • 13
  • 27
  • I'm using a div to show the user a notice (so instead of the alert, I use jQuery show function to show a div). Is there a way to have the div disappear when the user types below 300? – Jason Sons Dec 21 '16 at 19:19
  • @JasonSons `else { $("#alertdiv").hide(); }` – Barmar Dec 21 '16 at 19:21