Th...">

How to change only HTML text?

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()Modifies all HTML content, including the tag <p>.

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

+5
source share
5 answers

Use idfor yours <p>and use the method .text():

<p id="needed">Please change me</p>
$('#needed').text('new text');
+9
source

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>.');
+3
source
$('p').html('changed')

'nuff

+1
$(function(){
    $('p').text('me');
})
+1
$('p').text('This has been changed')

API jQuery.

0

All Articles