0

I want to make letters like 'å ä ö' visible. I need to replace these letters with ascii code, I guess.

I have tried jquery and javascript, but it did not work. Look at the following code please:

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

    <script>


    jQuery("#body").html(
    jQuery("#body").html().replace('ä', '&aring;')
);


     document.body.innerHTML = document.body.innerHTML.replace(/ä/g, '&aring;');



    </script>



  </head>

  <body id="body">

    <div class="blog-masthead">
      <div class="container">
        <nav class="blog-nav">
          <a class="blog-nav-item active" href="index.php">Inlägg</a>
        </nav>
      </div>
    </div>
LaurentY
  • 7,495
  • 3
  • 37
  • 55
Danny
  • 27
  • 8
  • Where exactly are the letters not displaying correctly? Following jsfiddle example shows that it should not be an issue to display these letters: https://jsfiddle.net/yceaeqyj/ – Ruben Pirotte Aug 16 '16 at 12:59
  • I want the js code to apply on everything between

    tags. And I want to repleace all of ' ä ' letters to ' å'. Is there a better way for doing this? Is my js code wrong?

    – Danny Aug 16 '16 at 13:38
  • 1
    I am not sure about your js code but @Déric Dallaire answers seems good on that area. However I think you should tell us why you are trying to change the letters like that, because maybe the issue can be avoided in a better way. – Ruben Pirotte Aug 16 '16 at 13:53
  • It is true. It works fine now. Thanks – Danny Aug 16 '16 at 15:12

1 Answers1

2

You can achieve what you want using one of the three methods below.

codepen

JQuery

// using a regex on the first parameter of replace, 
// picks all the 'ä' instead of the first one 
var replaced = $("body").html().replace(/ä/g,'&aring;'); 
$("body").html(replaced);

JavaScript

// using a regex on the first parameter of replace, 
// picks all the 'ä' instead of the first one 
document.body.innerHTML = document.body.innerHTML.replace(/ä/g, '&aring;');

Better Solution

A better alternative to the two previous code sample is to convert your file to the right encoding. In order to do that, make sure you this snippet in the head of you HTML document.

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

If that's not working, you also have to make sure the file is saved with encoding UTF-8. If you're using Notepad++, this is done via Encoding > Encode in UTF-8.

Der
  • 727
  • 7
  • 20