Chapsterj
Chapsterj

Reputation: 6625

remove br tags and leave all other tags

I just want to remove the br tags.

'<div class="temp">some content<br>and more<br><a href="#"> and more content</a></div>'

$('.temp br').contents().unwrap();

why doesn't this work.

I also tried this as I saw it on stackoverflow but didn't work either. What am I doing wrong with both of these.

$('.temp').contents().filter(function() {
  return this.nodeType == 3;
}).filter('br').remove();

Upvotes: 1

Views: 3308

Answers (3)

karim79
karim79

Reputation: 342695

That doesn't work because it's wrong. Try this:

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

Demo.

To replace them with spaces:

$('.temp br').replaceWith("&nbsp;");

Demo.

Upvotes: 2

Zhenya
Zhenya

Reputation: 220

$('.temp br').before('&nbsp;').remove();

http://api.jquery.com/before/
http://api.jquery.com/remove/

Upvotes: 0

Robert
Robert

Reputation: 8767

Here is a working solution: http://jsfiddle.net/p6Mzu/. There is also a CSS only solution that is commented out in the link, as this only removes them. The jQuery replaces the <br> with &nbsp;.

jQuery:

$(".temp br").replaceWith("&nbsp;");

CSS:

.temp br { display: none; }

Upvotes: 0

Related Questions