rarksoca
rarksoca

Reputation: 98

How to remove <br> using jquery

This is my html

<div class="ch-list">
   <p><a>Ch 1</a><br><a>Ch 2</a></p>
   <br>
   <p><a>Ch 3</a><br><a >Ch 4</a></p>
</div>
<div class="pg-list">
   <p><a>pg 1</a><br><a>pg 2</a></p>
   <br>
   <p><a>pg 3</a><br><a>pg 4</a></p>
</div>

If I do the following

$("p,.ch-list").children('br').remove();

it remove all < br > It become like this

<div class="ch-list">
   <p><a>Ch 1</a><a>Ch 2</a></p>
   <p><a>Ch 3</a><a>Ch 4</a></p>
</div>
<div class="pg-list">
   <p><a>pg 1</a><a>pg 2</a></p>
   <p><a>pg 3</a><a>pg 4</a></p>
</div>

i don't know how to only remove < br > between < a > in class=ch-list not include other < br > between < p > or < br > from other class i want to make something like this

<div class="ch-list">
   <p><a>Ch 1</a><a>Ch 2</a></p>
   <br>
   <p><a>Ch 3</a><a>Ch 4</a></p>
</div>
<div class="pg-list">
   <p><a>pg 1</a><br><a>pg 2</a></p>
   <br>
   <p><a>pg 3</a><br><a>pg 4</a></p>
</div>

Please tell me how to do Sorry for my bad english

Upvotes: 0

Views: 58

Answers (2)

Prasanta Mallick
Prasanta Mallick

Reputation: 46

$(".ch-list p").children('br').remove();

Upvotes: 1

You could do it like this, since you say you want to remove <br> that is between 2 x <a>:

$('.ch-list p br').filter(function() {
  if ($(this).prev().is("a") && $(this).next().is("a")) {
    return this
  }
}).remove()

Demo

$('.ch-list p br').filter(function() {
  if ($(this).prev().is("a") && $(this).next().is("a")) {
    return this
  }
}).remove()
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="ch-list">
  <p><a>Ch 1</a><br><a>Ch 2</a></p>
  <br>
  <p><a>Ch 3</a><br><a>Ch 4</a></p>
</div>
<div class="pg-list">
  <p><a>pg 1</a><br><a>pg 2</a></p>
  <br>
  <p><a>pg 3</a><br><a>pg 4</a></p>
</div>

Upvotes: 1

Related Questions