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

x
?
Solved

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

Posted on 2011-03-01
4
Medium Priority
?
734 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 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

Your business may be under attack from a silent enemy that is hard to detect. It works stealthily in the shadows to access and exploit your critical business information, sensitive confidential data and intellectual property, for commercial gain. T…
Do online retailers and e-commerce sites even need to bother with the season? Is it lucrative to update and prepare your site for Black Friday and Cyber Monday shoppers? We'll find out now.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

876 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