Solved

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

Posted on 2011-03-01
4
710 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
[X]
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
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Time picker 4 472
Javascript onReadyStateChange event not fire for XMLHttpRequest object? 2 291
Javascript simple function doesn't work 6 116
Javascript -- "RIGHT" function ? 3 171
Read the original post on Monitis Blog. Believe it or not, the most important thing about the website of your business is not what’s on it but how fast it loads. Yes, that’s right!    As you can see on this infographic (an oldie but goodie!), …
This article discusses how to implement server side field validation and display customized error messages to the client.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

730 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