Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

jQuery - How to add a class to each td of a table

Posted on 2011-03-01
14
Medium Priority
?
779 Views
Last Modified: 2012-05-11
I would like to dynamically a class to each corner td in a table of an unknown number of columns and rows.

I need some thing like:

if table class ="rounded_corners"

if 1st td add class "td-top_left"
if last td in 1st row add class "td_top_right"
if 1st td in last row add class "td-bottom_left"
if last td in last row add class bottom_top_right"

Can you help?
0
Comment
Question by:Luv2Muff
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 4
  • +1
14 Comments
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 35006834
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35006839
Try something like:
<script type="text/javascript">
   $("tr:first td:first").addClass("td-top_left");
   $("tr:first td:last").addClass("td_top_right");
   $("tr:last td:first").addClass("td-bottom_left");
   $("tr:last td:last").addClass("bottom_top_right");
</script>

Open in new window

0
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 35006852
$("td:first-child").addClass("td-top_left");
$("tr:first-child td:last-child").addClass("td_top_right");
$("tr:first-child td:first-child").addClass("td_bottom_left");
$("tr:last-child td:last-child").addClass("td_bottom_right");
0
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
LVL 16

Expert Comment

by:Swapnil Piparia
ID: 35006911
Hi Luv2Muff,

Here is your solution
$(".rounded_corners tr:first td:first").addClass("td-top_left");
 $(".rounded_corners tr:first-child  td:last-child").addClass("td_top_right");
 $(".rounded_corners tr:last-child  td:first-child").addClass("td_bottom_left");
 $(".rounded_corners tr:last-child  td:last-child").addClass("td_bottom_right");
 

Open in new window


Here is link to working code snippet.
http://jsfiddle.net/vmnDQ/

Thanks,
netswap
0
 

Author Comment

by:Luv2Muff
ID: 35007654
@Netswap

That code snippet works, but break if you have multiple tables on a page.

Can you make it work with multiple tables?

THANKS
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35007681
If you want to target a specific table you need to add its ID to the selector. Something like:
<script type="text/javascript">
   $("#yourTableID tr:first td:first").addClass("td-top_left");
   $("#yourTableID tr:first td:last").addClass("td_top_right");
   $("#yourTableID tr:last td:first").addClass("td-bottom_left");
   $("#yourTableID tr:last td:last").addClass("bottom_top_right");
</script> 

Open in new window

0
 

Author Comment

by:Luv2Muff
ID: 35007721
rather than target one particular table, I want it to target all tables on  a page with the same css class
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 1000 total points
ID: 35007834
Try:
   $(".rounded_corners").each(function() {
       $("tr:first td:first", $(this)).addClass("td-top_left");
       $("tr:first td:last", $(this)).addClass("td_top_right");
       $("tr:last td:first", $(this)).addClass("td-bottom_left");
       $("tr:last td:last", $(this)).addClass("bottom_top_right");
   });

Open in new window

0
 
LVL 16

Assisted Solution

by:Swapnil Piparia
Swapnil Piparia earned 1000 total points
ID: 35007870
Hi Luv2Muff,

replace first line
   $(".rounded_corners tr:first td:first").addClass("td-top_left");

Open in new window

with
 $(".rounded_corners tr:first-child td:first-child").addClass("td-top_left");

Open in new window


will solve your problem.

Thanks,
netswap
0
 

Author Comment

by:Luv2Muff
ID: 35008442
Thanks for you help!

@carl & @net happy to share 50/50?
0
 
LVL 16

Expert Comment

by:Swapnil Piparia
ID: 35009404
Hi Luv2Muff,

        Solution provided by carl (accepted one) is not working . Please check http://jsfiddle.net/vmnDQ/2/.

Thanks,
Netswap.
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35009445
Only because I copied the class names from the original post, when you've quite finished being pedantic.
0
 

Author Comment

by:Luv2Muff
ID: 35009684
Did not mean to upset any one, but it did work for me.

How about getting all the points on this one:

http://www.experts-exchange.com/Programming/Languages/Scripting/JScript/Q_26855564.html#a35008837
0
 
LVL 16

Expert Comment

by:Swapnil Piparia
ID: 35010002
@carl,

        I haven't replied to accepted post with intention of getting points. However when I was testing your solution it was not working that's why I have just pointed it out. If it is working for Luv2Muff then that's what all we want.

@Luv2Muff,

       Thanks but I am happy with whatever points you have given to us.

Thanks,
Netswap.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question