Matt S
Matt S

Reputation: 91

JQuery show div if field value < n and NOT empty

I am trying to work out how to show a div based on input to a field. I want to show the div if both of these conditions are true:

1) the field value is <=30 2) the field is not empty

at the moment i i have figured out the <=30 bit but then the form assumes that empty is 0 and shows the div, but i dont want the div to show if the field is left blank.

I tried this but it doesnt work:

 $("#hidden")[$(this).val() <= "30" and !="" ? 'show' : 'hide']("fast");

Here's what i have so far: http://jsfiddle.net/q5kKz/318/

Thanks for any help!

Upvotes: 0

Views: 1350

Answers (4)

TecHunter
TecHunter

Reputation: 6141

Try this one :

http://jsfiddle.net/q5kKz/323/

this "===" is important in string comparison :

$(this).val() !== "" && $(this).val()*1 <= 30

You should also consider using other events like keypress or the JS will only trigger when it loses the focus.

Upvotes: 0

Adil Shaikh
Adil Shaikh

Reputation: 44740

Demo --> http://jsfiddle.net/q5kKz/322/

use && not and

$('#Severity-of-your-symptoms').change(function() {
  $("#li-10-14")[this.value <= "30" && this.value != '' ? 'show' : 'hide']("fast");
}).change();

Upvotes: 0

vlio20
vlio20

Reputation: 9305

Maybe the problem is tha 30 is a string so you need to cast it to int, like this

$("#hidden")[parseInt($(this).val()) <= 30 && !="" ? 'show' : 'hide']("fast");

hope it will work for you!

Upvotes: 0

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382514

Fix the syntax :

 var val = this.value;
 $("#hidden")[(val !=='' && val <= 30) ? 'show' : 'hide']("fast");

Demonstration (I fixed a few other problems)

Upvotes: 3

Related Questions