tags:

views:

67

answers:

4

My customer's CMS from the last century outputs the following code.

And I'd like to remove only the first two BR tags with jquery.

<div id="system">
<BR CLEAR="ALL"><BR>// I want to remove both BR.
...

<BR>...
...
<BR>

I assume something like this. But I am not sure.

$('#system br').remove();

Could anyone tell me how to do this please?

Thanks in advance.

+1  A: 

Try

$('#system br:first').remove();
$('#system br:first').remove();

The first line removes the first br, then the second br becomes the first, and then you remove the first again.

BrunoLM
+2  A: 
$("#system br:lt(2)").remove();
Magnar
+9  A: 

Use :lt():

$('#system br:lt(2)').remove();

:lt() takes a zero-based integer as its parameter, so 2 refers to the 3rd element. So :lt(2) is select elements "less than" the 3rd.

Example: http://jsfiddle.net/3PJ5D/

Andy E
forgot about `lt` and `gt` +1 for smallest example.
RobertPitt
woah. i didn't even realize this existed.
David Murdoch
@David - Haha. I'm in the same boat. I love how I learn something new on SO every day. @Andy - Thanks! +1
JasCav
+1  A: 

Also try nth-child.

$("#system > br:nth-child(1), #system > br:nth-child(2)").remove();​

removes first and second instance of br within #system

RobertPitt
Note that if there were further child elements with `<br>` elements inside them, those would be removed too. You could work around this using the immediate child selector, `>`.
Andy E
great point +1.
RobertPitt
@Robert: and +1 for your edit :-)
Andy E