Solved

jQuery - Add a different class if only one td in a row

Posted on 2011-03-01
4
699 Views
Last Modified: 2012-05-11
Thanks to some earlier help, I have the following code.

Sometimes the top and bottom rows of my tables only contain one cell/td.

Can the code be amended so that:

1) If the top row only contains one td it adds a class of td_top

2) If the top row only contains one td it adds a class of td_bottom

Then if it contains more than one, it still adds both left and right.

Hope that makes sense.
$jQ(document).ready(function() {//Add classes to the for corner td's of a table
   $jQ(".css3").each(function() {
      $jQ("tr:first td:first", $jQ(this)).addClass("td_top_left");
      $jQ("tr:first td:last", $jQ(this)).addClass("td_top_right");
      $jQ("tr:last td:first", $jQ(this)).addClass("td_bottom_left");
      $jQ("tr:last td:last", $jQ(this)).addClass("td_bottom_right");
   }); 
});

Open in new window

0
Comment
Question by:Luv2Muff
  • 2
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
OblivionSY earned 500 total points
ID: 35008683
var count = $jQ("tr:first >td").length;
if(count > 1)
    //DO as existing
else
   //Add your single class
0
 

Author Comment

by:Luv2Muff
ID: 35008837
Unsure if I have implemented that correctly:?
$jQ(document).ready(function() {//Add classes to the for corner td's of a table
$jQ(".css3").each(function() {
var count = $jQ("tr:first >td").length;
if(count > 1;
$jQ("tr:first td:first", $jQ(this)).addClass("td_top_left");
$jQ("tr:first td:last", $jQ(this)).addClass("td_top_right");
else;
$jQ("tr:first td", $jQ(this)).addClass("td_top");
var count = $jQ("tr:last >td").length;
if(count > 1;
$jQ("tr:last td:first", $jQ(this)).addClass("td_bottom_left");$jQ("tr:last td:last", $jQ(this)).addClass("td_bottom_right");
else;
$jQ("tr:last td", $jQ(this)).addClass("td_bottom");
}); 
});

Open in new window

0
 

Author Closing Comment

by:Luv2Muff
ID: 35011464
Sussed it!

Thanks.
0
 
LVL 3

Expert Comment

by:OblivionSY
ID: 35012559
Great, glad to help :)

You don't need to var the "count" twice.

if syntax (just for post clarity) should be

if (condition) {
 // do something
} else if (otherCondition) {
 // do something else
} else {
 // otherwise do this
}

Ie, does not have ; at the end of the "if" lines
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Resources for learning AutoLISP 1 65
Reorder multi file names 17 67
JavaScript issue with comma separated values 12 74
Drag and drop columns in Wordpress WPDataTables 9 201
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
As a business owner, there are many things that keep you up at night. Profit margins, employee retention, human resource protocols, whether your product or service will remain competitive. When you own or manage a technology company that operates la…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

777 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