Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change HTML text only?

How to change this?

<p>Please change me</p>

Can I change "Please change me" without using replaceWith("<p>This is been changed.</p>")? replaceWith() is changing the whole HTML content including the <p> tag.

Is there a way to get the "Please change me" only? And not including the <p> tag... then change it to something like "This is been changed."

like image 337
Ryan Avatar asked Jul 17 '11 06:07

Ryan


2 Answers

Use the id for your <p>, and use the .text() method:

<p id="needed">Please change me</p>
$('#needed').text('new text');
like image 137
VMAtm Avatar answered Sep 21 '22 19:09

VMAtm


Just do

// I'm just changing the text content
$('p').text('This has been changed.');

or even

$('p').html('This has been changed.');

The latter should really be used only if your content contains HTML markup, for example:

$('p').html('<span>This has been <strong>changed</strong></span>.');
like image 33
Cᴏʀʏ Avatar answered Sep 19 '22 19:09

Cᴏʀʏ