user12345
user12345

Reputation: 2418

How to remove br using jquery?

My structure is like this:

<p>something</p>
<br/>
<br/>
<p>ssddfgdfg</p>
<br/>
<br/>
<p>dsdfsfsdfsdfsd</p>
<br/>
<br/>

I want to remove one br tag whenever 2 br tag comes together, like this:

<p>something</p>
<br/>
<p>ssddfgdfg</p>
<br/>
<p>dsdfsfsdfsdfsd</p>
<br/>

How can I achieve this using jQuery?

Upvotes: 2

Views: 4658

Answers (4)

AbhiNickz
AbhiNickz

Reputation: 1113

I am using this code

 $('br').each(function () 
    {
      if ($(this).next().is('br')) 
      {
        $(this).next().remove();
      }
    }

Upvotes: 0

Mr.T.K
Mr.T.K

Reputation: 2356

Another solution,

$('br').prev('br').remove();

Upvotes: 1

mrtsherman
mrtsherman

Reputation: 39882

Try the immediate sibling selector

http://jsfiddle.net/u2vS6/

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

Upvotes: 8

apnerve
apnerve

Reputation: 4829

You can try doing

$('br').next('br').remove();

This will remove the second <br/>. There might be a better solution though.

EDIT: As I said other answers were better :)

Upvotes: 3

Related Questions