0

I have the following html

<div class="ui-widget" style="margin-top: 1em; font-family: Arial">
   Selected:
<div id="locationLog" style="height: 100px; width: 200px; overflow: auto;" class="ui-widget-content"></div>
</div>

<input type="hidden" name="HiddenLocations" id="HiddenLocation" />

I then have some code that runs each time the page loads to add some text into the locationLog div.

@if (Model.SearchCriteria != null && Model.SearchCriteria.Locations != null)
{
   foreach (string t in @Model.SearchCriteria.Locations)
   {
      <script type="text/javascript">
         $("<div/>").text('@t').appendTo("#locationLog");
         $("<br/>").text("").appendTo("#locationLog");
         $("#locationLog").scrollTop(0);

         selectedLocations = $('#locationLog' + ' div').map(function () {
            return $(this).text();
         }).get();

         $('input[name=HiddenLocations]').val(selectedLocations); 
      </script>
   }
}

Now, this code works well when I have text such as 'London' but when I have 'Reykjavík', this get's written as 'Reykjav&#237;k'. How can I write out the correct text?

DrColossos
  • 12,656
  • 3
  • 46
  • 67
Sachin Kainth
  • 45,256
  • 81
  • 201
  • 304

1 Answers1

1

Try declaring utf-8 charset in your document

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

EDIT: Try this:

function htmlDecode(input){
  var e = document.createElement('div');
  e.innerHTML = input;
  return e.childNodes.length === 0 ? "" : e.childNodes[0].nodeValue;
}

and use it here:

selectedLocations = $('#locationLog' + ' div').map(function () {
   return htmlDecode($(this).text());
}).get();

Source: Unescape HTML entities in Javascript?

Community
  • 1
  • 1
Ratzor
  • 307
  • 3
  • 15