Shahzad
Shahzad

Reputation: 1415

Datatable Jquery special character encoding and decoding to HTML

I found out that html special characters such as '&' are not being decoded when we fetch value from the table object using API functions but instead of they are in ASCII or Unicode form. This is my simple initialization for the dataTable.

var otable = $('#vtable').DataTable({
        "dom": '<"top"lBf<"clear">>rt<"bottom"ip<"clear">>'
}); 

See this Fiddle.

What should be best solution to decode html special character before passing it to the data processing?

Upvotes: 1

Views: 5341

Answers (2)

Gyrocode.com
Gyrocode.com

Reputation: 58900

Encode HTML entities:

var valEncoded = $('<div>').text(val).html();

Decode HTML entities:

var valDecoded = $('<div>').html(val).text();

See this example for code and demonstration.

See updated jsFiddle for demonstration on how it could be used in your project.

Upvotes: 2

Shahzad
Shahzad

Reputation: 1415

Using this answer of Stackoverflow, the solution for question is the function below to decode the html characters from a row of data.

function decodeHtml(str) {
    var map =
    {
        '&amp;': '&',
        '&lt;': '<',
        '&gt;': '>',
        '&quot;': '"',
        '&#039;': "'"
    };
    return str.replace(/&amp;|&lt;|&gt;|&quot;|&#039;/g, function (m) { return map[m]; 
    });
}

Upvotes: 0

Related Questions