Reputation: 986
I'm looking for a way to remove one of two identical divs if they are side by side:
Example:
<div id="foo">
<ul>
<li><a class="foo2" href="/">Home</a></li>
<div class="foo3"></div><div class="foo3"></div>
<li><a class="foo2" href="link.html"><strong>Link text</strong></a></li>
</ul>
</div>
In the above example two .foo3 links are present, while only one is needed.
Basically something like:
$("#foo ul li:contains('div.foo3 div.foo3')").first().remove();
Upvotes: 1
Views: 1532
Reputation: 1065
Try this:
var i = 0;
$(".foo2").each(function() {
if (i >= 1){
$(this).remove();
}
i++;
});
Upvotes: 0
Reputation: 20737
I think you are searching for something like .each()
(docs) or .filter()
if you have a clue how to select all but the first element. (docs).
$('#foo ul li div.foo3').each( function( i, elem ) {
if( i != 0 ) {
$(elem).remove();
}
} );
Upvotes: 0
Reputation: 21694
How about this?
$('#foo ul li').each(function() {
if ($(this).find('div.foo3').length > 1)
$(this).find('div.foo3:not(:first)').remove();
});
Also, your .foo3
s aren't in an li
, they're in the ul
.
Upvotes: 1
Reputation: 41605
Simple way:
$('.foo3').last().remove();
Living example: http://jsfiddle.net/PPBt2/1/
Upvotes: 0
Reputation: 4807
It's simple, Try
$('div.foo3').eq(1).remove();
JSFIDDLE - http://jsfiddle.net/rUvS5/1/
Upvotes: 3