Great to follow this thread... Very concise and elegant jQuery code!! thanks ----- Original Message ----- From: Charlie To: jquery-en@googlegroups.com Sent: Wednesday, June 24, 2009 1:02 PM Subject: [jQuery] Re: Selecting the first of two td's in a tr
actually i learned how to add the (......., this) from another post on this list i struggeled at first learning how to use "THIS" when creating a selector I always wanted it to work like a parent child regular selector and kept trying a lot of things like: $(this "li").hide(); or $((this)."li").hide() or $("(this) li").hide() of course none of the variations worked. Finally when find(), or children() sunk in it was far more intuitive. Then recently when $("selector",this) came up I realized it saved a bit more virtual ink. >From a chaining perspective it's easier to read the extra step of find() or children() and that is likely why don't see it in docs much Matthew wrote: Sweet, thanks Charlie... Charlie, how did you find out about: $("td:first",this)? In the jquery docs i havent seen the ",this" I'm assuming that adding that filters out the first td of each tr, I'd be interesting in knowing more about how to filter things. Looks like I could make most of my code way more efficient. On Jun 24, 11:35 am, MikeyJ <m.en...@gmail.com> wrote: No prob! Looks like you are real close to what Charlie just posted that works great, except that it applies to every table on the page. Once I added the table id it was perfect! $("table#mytableid tr").each(function() { $("td:first",this).attr("align","right"); }); On Jun 24, 11:31 am, Matthew <mvbo...@gmail.com> wrote: sorry about the error, replace "break;" with "return false;" Yeah, ignore the last code then. Try this, I'm trying to find a way of doing this cleanly so I guess we are both learning together haha $("table tr").each(function(){ $(this).children(":first").attr("attribute","value"); }); On Jun 24, 11:14 am, MikeyJ <m.en...@gmail.com> wrote: Looks like your first two offerings throw this error in Firebug: "unlabeled break must be inside loop or switch" Your last example works but it only works on the first TR and it affects both TD's in that TR. ?? On Jun 24, 10:59 am, Matthew <mvbo...@gmail.com> wrote: yeah haha let me re-do: $("table#id tr:first-child").each(function(){ $(this).attr("attribute","value"); }); try that. I am thinking that the tr:first-child should return the TD for every TR in the table. Im not sure how this will work if you have nested tables. I'm at work so I cant spend the time to test the code before hand. On Jun 24, 10:50 am, MikeyJ <m.en...@gmail.com> wrote: Thx Matthew! Great explanation. I probably should have worded one thing a bit differently...I'd like to set this attribute for "the first TD in each TR for EVERY TR in an entire table". I'm sure this changes things a tiny bit? Mike On Jun 24, 10:43 am, Matthew <mvbo...@gmail.com> wrote: when you use this code $("tr td") it would create an array of all those td's in the tr. So then we just need to cycle through the first two and set the attribute then break the cycle. var counter = 0; $("tr td").each(function(){ $(this).attr("attribute","value"); counter++; if(counter > 1) { break; } // after the first two td's stop the iteration through all the td's. }); You can be more specific with what tr you use or you could use a table with a certain class or id: $("table.class tr td").each(........ hope this helps. On Jun 24, 10:26 am, MikeyJ <m.en...@gmail.com> wrote: Hi All, I'd like to set the align attribute of only the first TD in a TR for an entire table but am not sure how to address them all in one go. Probably an Nth child thing or similar but not sure! Thx, Mike