Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-03-01
14
Medium Priority
?
781 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
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…
Suggested Courses

972 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