Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

insert text .before() INSIDE the tag with jquery

Tags:

jquery

insert

here's a basic example of what I'm trying to do:

<div class="container">
  <h2>Greetings</h2>
  <p>All this content, including the HTML comes from a CMS</p>
</div>

I want to insert some more content from the CMS like so

<div class="container">
  <h2>Greetings</h2>
  <p>NEW YORK - December 29, 2010 - All this content, including the HTML comes from a CMS</p>
</div>

This tag won't work, as it'll put it before the

tag...

$('.container p').before('NEW YORK - December 29, 2010');

so how can i insert it into the tag? I can't tag that p with a class or put a marker inside the

tag since it is being spit out of a CMS.

like image 572
panzhuli Avatar asked Sep 07 '26 23:09

panzhuli


2 Answers

I found the prepend(), which seems to do what I want. That is another solution as well. Thanks!! http://api.jquery.com/prepend/

like image 189
panzhuli Avatar answered Sep 10 '26 15:09

panzhuli


Use text().

var oldText = $('.container p').text();
$('.container p').text("NEW YORK - December 29: " + oldtext);
like image 20
Alex Weinstein Avatar answered Sep 10 '26 14:09

Alex Weinstein



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!