Jump to content
babyboomer

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

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?

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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!

Share this post


Link to post
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;".

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...