Saeed Neamati
Saeed Neamati

Reputation: 35842

Finding successive <br /> elements, using jQuery

Imagine that I have this HTML snippet:

<div></div>
<br />
<div></div>
<br />
<img src='' alt='' />
<p></p>
<br />
<br />
<br />
<div></div>
<a href=''></a>
<br />

Which has no definite rule at all. The only thing I know, is that 3 consecutive <br /> elements exist somewhere. Now, I need to find the three consecutive <br /> elements using jQuery, and remove anything after them.

How can I do that?

Upvotes: 0

Views: 748

Answers (1)

RightSaidFred
RightSaidFred

Reputation: 11327

$('br + br + br').nextAll().remove();

If there are potential text nodes to be removed, do this:

var el = $('br + br + br')[0],
    nxt;

while( nxt = el.nextSibling ) { 
    el.parentNode.removeChild( nxt );
}

Or with more jQuery:

var el = $('br + br + br')[0],
    nxt;

while( nxt = el.nextSibling ) { 
    $( nxt ).remove();
}

Upvotes: 5

Related Questions