the .val() method normally returns a string. So, the comparison is sorta like "5" > 5 - which is a little off. To be complete, you should parse out the number value you want: if (parseInt(this.val() ) > 5) { . . . }
Or use parseFloat if you're after decimal values. This is one of those subtle things that can cause grief. One would think that "5" > 5 would be automagically translated to 5 > 5. And it sometimes is. but what if the value was " 5 "? (notice the spaces). I've been bit by this one a few times.... :) But I'm not saying that is THE source of your issues.. just a potential... :) Shawn dug wrote: > Hi Kelvin, > > This looks really good. > > It's not working but this may be down to something really stupid on my > part. I've put your code into an example to give an idea of what I'm > trying to do: > > http://donkeyontheedge.com/jqtest/ (I'm looking at this in Firefox) > > Could it be that $this.val() doesn't return an integer that > corrresponds to the number of letters in the value attribute? Should > it be val().length() or something? > > Thanks for your help! > > Cheers, > Dug > > > On Dec 31, 12:06 pm, Kelvin Luck <[EMAIL PROTECTED]> wrote: >> Hi, >> >> You could try this (untested): >> >> $('[EMAIL PROTECTED]').each( >> function() >> { >> $this = $(this); >> if ($this.val() < 5) { >> $this.addClass('S'); >> } else if ($this.val() > 10) { >> $this.addClass('L'); >> } else { >> $this.addClass('M'); >> } >> } >> ); >> >> Hope that helps, >> >> Kelvin :) >> >> Dug Falby wrote: >>> Hi all, >>> I'd like to do the following: >>> walk through the DOM stopping at: >>> [EMAIL PROTECTED] >>> and conditionally add a class to each input tag: >>> if [EMAIL PROTECTED]() < 5 then .addClass('S') >>> if [EMAIL PROTECTED]() > 10 then .addClass('L') >>> else .addClass('M') >>> Can I still use the lovely $() construct? >>> Thanks:-) >>> Dug