Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding <br /> before specific part of text using jQuery

Tags:

html

jquery

I have 2 paragraphs like this :

<p>text bla bla bla owner of the idea : John Smith</p>
<p>text bla bla bla bla bla bla owner of the idea : James Marino</p>

Is there any way I could use jQuery to add a new line before "Owner of the idea" till the end of the sentence?

Thanks in advance

like image 545
Ahmad Alfy Avatar asked Jun 16 '11 22:06

Ahmad Alfy


3 Answers

If this is a copy of your jquery script, try using a \n for new line. That work in javascripts.

jQuery WORKING example:

 <p id="s">text bla bla bla owner of the idea : John Smith</p>
<p>text bla bla bla bla bla bla owner of the idea : James Marino</p>

<script type="text/javascript">
    $(document).ready(function() {
        $("p").each(function() {
            var getContent=$(this).text();
            var newString=getContent.replace('owner of the idea','<br />owner of the idea');
            $(this).html(newString);
        });
    });
</script>
like image 175
RRStoyanov Avatar answered Sep 22 '22 13:09

RRStoyanov


If the pattern is consistent, you could do a replace based on the : character.

 $('p').each(function(){ 
          this.html(this.html().replace(":",":<br />"));
 }); 

Update

According to the comment, it looks like I misread the question. You can still use this same strategy however,

  var p = $('p:first');
  p.html(p.html().replace("owner of the idea", "<br />owner of the idea"));
like image 23
smartcaveman Avatar answered Sep 24 '22 13:09

smartcaveman


$('<br />').insertBefore(selector for your paragraph);

I don't know what your surrounding markup looks like, so it's not clear what the best selector for your paragraph would be.

like image 34
kinakuta Avatar answered Sep 24 '22 13:09

kinakuta