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
Solved

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

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
The Enter button on the keyboard 5 328
Trying to format phone number on keyin. 5 95
IE 11 javascript function not working 13 165
Javascript -- "RIGHT" function ? 3 148
If you are looking at this article, you have most likely been hit by some version of ransomware and are trying to find out if there is anything you can do, or what way you should react - READ ON!
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…

829 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