Jump to content

Can I pass an HTML character entity to the jquery text() method?


babyboomer
 Share

Recommended Posts

I would like to call the jquery text() method with a string argument that includes the HTML character entity for the copyright symbol (©). I've tried numerous variations of the following theme:

$('#copyright').text('©  ' + date.getFullYear() + ' John Doe');

So far, nothing I've tried has produced the desired result. Specifically, the HTML "& copy;" entity is not getting translated into the copyright symbol. Is it possible to pass an HTML character entity in this way?

Link to comment
Share on other sites

6 hours ago, dsonesuk said:

.text() will render that as a textual representation and renders '<' and '>' as &lt; and &gt; so &copy; will become &amp;copy; use .html() instead.

Okay. I'm not sure I follow all of that, but the bottom line is that I did try the .html() method…

    var d = new Date();
    $('#copyright').html('&copy;  ' + d.getFullYear() + ' John Doe');

… and it seems to be working! Thank you!

Link to comment
Share on other sites

Any html character codes (<, >, &) inserted into .text() is converted to html entity equivalent ('&lt', &gt; &amp;), if you use.

var a = "Hello World! & Universe! &copy;";
$('#copyright').text('<span>' + a + '</span>');

You will actually see in the browser page

<span>Hello World! & Universe! &copy;</span>

because it is not html now, it shows as html entities textual character equivalents "&lt;span&gt;Hello World! &amp; Universe! &amp;copy; &lt;/span&gt;".

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...