-1

I have an HTML element that I wish to modify via Javascript. It contains a special character ©.

If I leave the HTML alone, I get what I expect. In its original state, it is

<li id="cp">&copy; MyCompany</li>

and then the line renders as "© MyCompany".

I want to auto-update that to display a range of years as the calendar changes, so I added some Javascript to the document's onload() function.

var today = new Date();
var cptext = "&copy; 2011-" + today.getFullYear().toString() + " MyCompany";
$('#cp').text(cptext);

But then, when the page loads, the &copy; is left as its original text, so the user sees the original $copy; 2011-2015 MyCompany instead of what I want, which is "© 2011-2015 MyCompany". The automatic date range code works fine, but the normal symbol replacement goes away.

How can I get the symbol to be updated as it is in the static HTML when I use jQuery/Javascript to modify the DOM element?

I do not have the option of recoding the entire HTML page as PHP.

Logicrat
  • 4,438
  • 16
  • 22
  • 7
    Use the `.html()` method instead of the `.text()` method. A related question: [What is the difference between jQuery: text() and html() ?](http://stackoverflow.com/questions/1910794/what-is-the-difference-between-jquery-text-and-html) – Ram Oct 15 '15 at 18:58

1 Answers1

1
var today = new Date();
var cptext = "&copy; 2011-" + today.getFullYear().toString() + " MyCompany";
$('#cp').html(cptext);

you need to use $.html(htmlstring) instead.

http://jsfiddle.net/uffghLz6/

http://api.jquery.com/text/#entry-examples

text() strips any of the html and returns only the text. It's like using element.innerHTML vs element.innerText

Sean Wessell
  • 3,490
  • 1
  • 12
  • 20