Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
48
rated 0 times [  53] [ 5]  / answers: 1 / hits: 6205  / 10 Years ago, fri, april 4, 2014, 12:00:00

So I'm trying to run an each loop in jQuery that has an if statement to determine if the element it's on is the first-child of it's parent and also have the else statement following (this seems to be the difficult part) to have other code run for those.



Everything I've tried and found only seems to work without the if and else..



Any thoughts?


More From » jquery

 Answers
2

Given the following example:



<table>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</table>


there are a couple of ways to do it



$(table tr td:first-child).each(function() {
//perform actions on all of the first TD elements
});
$(table tr td:not(:first-child)).each(function() {
//perform actions on all of the other TD elements
});


Or



$(table tr td).each(function() {
var td = $(this);
if (td.is(:first-child)) {
//perform actions on all of the first TD elements
}
else {
//perform actions on all of the other TD elements
}
});

[#46273] Thursday, April 3, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jarettajb

Total Points: 678
Total Questions: 94
Total Answers: 90

Location: Guernsey
Member since Tue, Jul 6, 2021
3 Years ago
;