Interesting...

What if, instead of your if/else if/else statements, I used a SWITCH
statement. Given that there will be about 12 possible values, a switch
statement might be even faster. I'm just not sure how to test for that one
given this situation.

<!----------------//------
andy matthews
web developer
certified advanced coldfusion programmer
ICGLink, Inc.
[EMAIL PROTECTED]
615.370.1530 x737
--------------//--------->

-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
Behalf Of abba bryant
Sent: Monday, August 07, 2006 6:02 PM
To: discuss@jquery.com
Subject: Re: [jQuery] UPDATED - possibility complicated color
codingquestion, now with exa





> 4) I used  your code, but added in a for loop like so:
> function colorPop() {
>       var values = [25,50,75,100];
>       for (i=0;i < values.length;i++) {
>               $('.p'+values[i]).each(function(){
>                       var t = $(this);
>                       if (t.is('.p'+values[i])) {
>                               t.addClass('pLev'+values[i]);
>                       }
>               })
>       }
> }
>
Couldn't this be re-written as :
function colorPop() {
        var values = [25,50,75,100];
        for (i=0;i < values.length;i++) {
                $('.p'+values[i]).each(function(){
                        $(this).is('.p'+values[i]).addClass('pLev'+values[i]);
                })
        }
}

I think ( and I might very well be wrong ) but .is() is chainable.
Also, since you are already selecting every item on the page why not do a
tag selection on "div" and then in the each check to see if it has the
classes needed.

[pseudo]

var values = [25,50,75,100];
$("div").each( function () {
    t = $(this);
    if ( t.is('p'+values[0])
        t.addClass('pLev'+values[0]);
    elseif ( t.is('p'+values[1])
        t.addClass('pLev'+values[1]);
    elseif ( t.is('p'+values[2])
        t.addClass('pLev'+values[2]);
    else ( t.is('p'+values[3])
        t.addClass('pLev'+values[3]);
});

[/pseudo]

If you know you only have x fixed number of values the overhead of a loop is
not needed.
Also, you are doing one $ query per value and then doing another $ query
once for each result in your previous code. This is 4 passes to filter 25
values out of 100 each time. The above method is 1 pass and a second query
to have a $(this) object to compare on - each compared once instead of 4
times. I am not sure what is faster in practice but the above seems likely
to be quicker.

Also what might help is to give the container an id. And to perhaps give a
generic class to all the items that might be colored. This would give $ more
accurate hooks to search with. Smaller haystack = quicker returns.

A possible even simpler solution to your problem. If you know the number of
options, and you know the colors to display - why not make 2 style sheets
with your styling done and instead of letting the user generate class names
with js - let them switch stylesheets? Your elements could have all the
classes they need inserted however you like and the stylesheet could control
the display. The user controls the stylesheet.




--
View this message in context:
http://www.nabble.com/UPDATED---possibility-complicated-color-coding-questio
n%2C-now-with-example...-tf2065361.html#a5697523
Sent from the JQuery forum at Nabble.com.


_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/


_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/

Reply via email to