Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using jquery to replace a <br> with a space

Tags:

jquery

replace

I'm importing a facebook RSS feed and many of the posts are missing a title. So in that case I want to pull some content from the body and use it as the title. The problem is that the body content can look like this...

Lorem ipsum<br>www.youtube.com<br>dolor sit amet.

I can't simply remove the br tags because it will become a run on sentence. I would rather replace the tags with a single space or comma with a space. I've tried the following code, but it doesn't seem to be working.

$('.teaser-title br').replaceWith(' ');

Thoughts?

like image 459
Bryan Casler Avatar asked Jan 12 '12 20:01

Bryan Casler


2 Answers

Description

2 Possible problems

  1. You have no element with the class teaser-title.
  2. You don't wait till the DOM is fully buildt

Anyway, this works

Sample

<div class="teaser-title">
    Lorem ipsum<br>www.youtube.com<br>dolor sit amet.
</div>

<script>
$(function() {
   $('.teaser-title br').replaceWith(' ');
});
</script>

Check out this jsFiddle

like image 197
dknaack Avatar answered Oct 04 '22 04:10

dknaack


Posting because it can be done! Could use CSS :P

(only works in webkit, so not really recommended)

.teaser-title br{
    content:"";
}

.teaser-title br:after{
   content:" ";
}

Live Demo

like image 31
Loktar Avatar answered Oct 04 '22 02:10

Loktar