to a specific piece of text using jQuery I have two paragraphs:

text bla bla bla owner of the idea : John Smith

text ...">

Adding <br/"> to a specific piece of text using jQuery

I have two paragraphs:

<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 it possible to use jQuery to add a new line before the "Owner of the idea" to the end of the sentence?

Thanks in advance

+3
source share
6 answers

If this is a copy of your jquery script, try using \nfor a new line. This works 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>
+5
source

If the pattern is consistent, you can make a replacement based on the character :.

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

Strike>

Update

According to the comment, it looks like I misunderstood the question. However, you can use the same strategy,

  var p = $('p:first');
  p.html(p.html().replace("owner of the idea", "<br />owner of the idea"));
+5
source
$('<br />').insertBefore(selector for your paragraph);

, , , .

+1
$('p').each(function() {
    $(this).html($(this).text().replace(/\bowner of the idea/i,'<br/>$&'));
});
+1

Place the class on the gap, then show and hide as desired, for example:

HTML:

<br class="break" style="display:none;"/>

JQuery

$('.break').show();
$('.break').hide();
0
source

You just need to add <br />to the paragraph using this JavaScript:

$("p").prepend("<br />");

Refresh. I understand what you mean now ... Try this code:

var old=$("p").html();
$("p").html(old.replace(/owner/gi,"<br />owner");

Ad @ m

-1
source

All Articles