Mike, thanks so much for the advice! :o)

I guess I'm still fuzzy on when I can use 'this' as opposed to '$(this)'. I would love to use this.myAttr, but didn't think I could. I really, really appreciate you re-writing my code snippet to show me what you're talking about. I know that the 'this' vs. '$(this)' discussion was had not too long ago, but I didn't (or couldn't) pay too much attention at the time. If you'd rehash it for me, or point me to the old thread, I'd appreciate that too! :o)

Cheers,
Chris

Michael Geary wrote:
$(function(){

    $("div.OrderEntryListRow").not(".Selected").each(function(i){
        alert("before: " + $(this).attr("id"));
        $(this).attr("id", "Row_" + (i+1));
        $(this).html($(this).html() + ': ' + (i+1));
        alert("after: " + $(this).attr("id"));
    });

});

Any time you see that much repetition in a piece of code, you should think,
"That might be expensive - let's do it once instead of many times."

So the first response would be to call $(this) only once:

    $("div.OrderEntryListRow").not(".Selected").each(function(i){
        var $this = $(this);
        alert( "before: " + $this.attr("id") );
        $this.attr( "id", "Row_" + (i+1) );
        $this.html( $this.html() + ': ' + (i+1) );
        alert( "after: " + $this.attr("id") );
    });

But we can do even better here. There's no reason to use .attr() and .html()
at all:

    $("div.OrderEntryListRow").not(".Selected").each(function(i){
        alert( "before: " + this.id );
        this.id = "Row_" + (i+1);
        this.innerHTML += ': ' + (i+1);
        alert( "after: " + this.id );
    });

That is both faster and much simpler.

-Mike


_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/


--
http://www.cjordan.info

_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/

Reply via email to