Hi,

Your demo page gives me an error in the firebug console: "missing ) after argument list". After a closer look I realised that's because you aren't wrapping the stuff you are passing into the ready call in a function block...

Also, $this.val() will return the actual value of the input so if you want the length you will need to use the .length property.

Here is a working version of the script:

$(document).ready(
        function() {
                $('[EMAIL PROTECTED]').each(
                        function() {
                                $this = $(this);
                                var len = $this.val().length;
                                if (len < 5) {
                                        $this.addClass('S');
                                } else if (len > 10) {
                                        $this.addClass('L');
                                } else {
                                        $this.addClass('M');
                                }
                        }
                );
        }
);

If you don't already have it, I recommend you get firebug which would help you to debug issues like this,

Hope that helps,

Kelvin :)

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

Reply via email to