Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print special characters with jquery?

I am designing web page in slovak language. To be able to use meantioned language special characters such as á or ž, I am using this html code:

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

Now it works as expected but only when I hard code that kind of text into html file.

As soon as I use jquery to print them it breaks down and those characters are not correctly shown.

$("#myDiv").html("áž");

Am I supposed to specify something in jquery or is there another way to overcome this problem?

like image 911
Matúš Dúbrava Avatar asked Mar 12 '14 04:03

Matúš Dúbrava


People also ask

What is '$' in jQuery?

$ is pretty commonly used as a selector function in JS. In jQuery the $ function does much more than select things though. You can pass it a selector to get a collection of matching elements from the DOM. You can pass it a function to run when the document is ready (similar to body.

How to remove space and special characters in jQuery?

ready(function() { $('#company'). change(function(){ $('#compname'). html($('#company'). val()); }); });

How to avoid special characters in JavaScript?

Use the replace() method to remove all special characters from a string, e.g. str. replace(/[^a-zA-Z0-9 ]/g, ''); . The replace method will return a new string that doesn't contain any special characters.

How to remove html special characters in JavaScript?

Using the Replace() method We can use the replace() method to replace one character with another character. Here, We will replace all the special characters in the HTML string with their Unicode by using the replace() method.


1 Answers

You can pass the numeric entity for that character into the html() function to achieve that,

Try a sample,

$('body').html('&#926;');

DEMO

like image 185
Rajaprabhu Aravindasamy Avatar answered Sep 30 '22 19:09

Rajaprabhu Aravindasamy