views:

27

answers:

2

Hi All,

This seems to be defeating me at the moment but I don't think I can be a million miles away.

Basically I have a Age Verification form which just shows the year unless it's the year in which the day month would also affect the user meeting the age restriction, (in this case 1992 for 18) - in which case the day/month selects would also appear (fadeIn).

So here's the code I have:

$(document).ready(function(){
$('#dateob').hide();  //hide the div containing the month and day selects
$("#f_yob").change(function() {
//select change function - if the value is greater than 1992 show the other select boxes
if ("$('#f_yob').attr('value')>1992")  {
$('#dateob').fadeIn('slow');
}
//otherwise hide the day/month selects 
else {
$('#dateob').fadeOut('slow');
}
});
});

If anyone is able to tell me where I'm going wrong I'd really appreciate it!

Andy

+2  A: 

You just need to change your if to this:

if (parseInt($(this).val(), 10) > 1992)  {

In a <select> (or any other input type), you use .val() to get the value then use parseInt() since you're comparing it to a number (.val() returns a string).


For the "what's wrong?" part..syntax mainly, you just have a string in the if(), which if not empty is true as far as an if() check is concerned, instead you need an actual comparison there.

Nick Craver
thats not the case, please see my answer
rochal
@rochal - You're comparing a string to a number, this is rarely if ever a good idea... explain what's *wrong* with this answer? Note that I already explained what's wrong overall in the bottom half of the answer, the top is just a better solution.
Nick Craver
@Nick - you have edited your answer - originally you didn't remove apostrophes from his code so it was evaluating expression as a single string. Now it is ok. And you're right, I could probably add parseint.
rochal
@rochal - You can only edit an answer for 5 minutes...it was locked in 8 minutes before your comment :)
Nick Craver
That's ace thanks Nick! I had looked at parseInt. One thing I don't understand though is whats the '10' about?Works great - appreciate your help!
tbwcf
@tbwcf - It's so the integer is parsed correctly in the case that it starts with a 0 for example, but you should ask the accepted answer why it doesn't have the radix supplied, as a best practice you don't want to leave this out.
Nick Craver
+1  A: 

in the fifth line of your code you do something like this:

if ("$('#f_yob').attr('value')>1992") { }

I believe what you meant is:

 if ($('#f_yob').attr('value')>1992) { }

Or even better:

if (parseInt($('#f_yob').val()) > 1992)  { }

Edit 1 you can use .val() instead of .attr('value').

Edit 2: Added parseInt

rochal
Sorry Rochal, somehow I saw Nicks answer first somehow! I've tried all which work great! I knew I was pretty close - really appreciate the help - thanks!
tbwcf