Solved

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

Posted on 2011-03-01
4
685 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

In this article, you will read about the trends across the human resources departments for the upcoming year. Some of them include improving employee experience, adopting new technologies, using HR software to its full extent, and integrating artifi…
What is Backup? Backup software creates one or more copies of the data on your digital devices in case your original data is lost or damaged. Different backup solutions protect different kinds of data and different combinations of devices. For e…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now