Dan382
Dan382

Reputation: 986

Remove one of two divs if identical

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

Answers (5)

user1
user1

Reputation: 1065

Try this:

var i = 0;
$(".foo2").each(function() {
if (i >= 1){
    $(this).remove();
}     
 i++;
});

Upvotes: 0

Sumurai8
Sumurai8

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

casraf
casraf

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 .foo3s aren't in an li, they're in the ul.

WORKING DEMO

Upvotes: 1

Alvaro
Alvaro

Reputation: 41605

Simple way:

$('.foo3').last().remove();

Living example: http://jsfiddle.net/PPBt2/1/

Upvotes: 0

commit
commit

Reputation: 4807

It's simple, Try

   $('div.foo3').eq(1).remove();

JSFIDDLE - http://jsfiddle.net/rUvS5/1/

Upvotes: 3

Related Questions