3

I am trying to pull data from a data attribute of a link and decode the HTML in it, but it is not working.

Here is a JSFiddle of my code: http://jsfiddle.net/Kd25m/1/

HTML:

<a id="main" class="margin-right-5 no-underline" href="#" data-qid="0" data-name="Post\u0026#39;\u0026#39; \u0026#39;!@#$%^\u0026amp;*()_+{}|:\u0026quot;\u0026lt;\u0026gt;?,./;\u0026#39;[]\\212\u0026quot;\u0026quot;3\u0026quot;4567890-=\u0026#39;" data-caption="" data-description="Animals are generally considered to have evolved from a flagellated eukaryote.[39] \u0026#39;!@#$%^\u0026amp;*()_+{}|:\u0026quot;\u0026lt;\u0026gt;?,./;\u0026#39;[]\\21234567890-=\u0026#39;Their closest known living relatives are the ch...">
Alert Decoded HTML</a>

JS:

$('#main').click(function(e){
    e.preventDefault()
    //alert("AA");
    var name = $('#main').data('name');
    alert(name);
    var decoded = $("<div/>").html(name).text();
    alert(decoded);
});

Using this works if a string is put in the name var, but if I pull the value from the data attribute, it doesn't work anymore.

9
  • 4
    How does it not work anymore? Commented Jan 14, 2014 at 19:24
  • Where in that code do you expect the decoding to happen? Did you try a Google search for "javascript decode entities" or something similar? Commented Jan 14, 2014 at 19:25
  • @DrCord That's what he's asking, if I'm not mistaken. Commented Jan 14, 2014 at 19:25
  • 1
    @AndrewMcGivery var decoded = $("<div/>").html(name).text(); this line is supposed to decode the HTML, but when pulling from the data attr, it doesn't decode it. If I was to do something like var decoded = $("<div/>").html("\u0026#39;!@#$%^\u0026amp;*()_+{}|:\u0026quot;\u0026lt;\u0026gt;?,./;\u0026#39;[]\\212\u0026quot;\u0026quot;").text(); it would work, but the way it is, isnt working currently. Commented Jan 14, 2014 at 19:28
  • 1
    @DrCord the first alert should display the text while still encoded, the second alert should be displaying the decoded html. Commented Jan 14, 2014 at 19:30

2 Answers 2

3

Try to unescape the unicode characters first:

function convert(str){

    return str.replace(/\\u([a-f0-9]{4})/gi, function (found, code) {
      return String.fromCharCode(parseInt(code, 16));
    });

}

Next try to make the div and html trick:

var decoded = $("<div/>").html(convert(name)).text();
Sign up to request clarification or add additional context in comments.

6 Comments

tried it, but still doesn't decode the text after converting it.
@user3072169 looks like it's working: jsfiddle.net/Kd25m/11
@elchininet correct, but to be precise you are unescaping Unicode inside JavaScript string, not "converting unicode to html".
Yep its working, my mistake. Thanks! Any explanation why this fixed it and wasn't working before?
That's right @Pavlo. thanks, i'll edit the response. Forgive my poor english
|
0
$('#main').click(function (e) {
    e.preventDefault()
    //alert("AA");
    var name = $('#main').data('name');
    var x = $("<p>").html(name).text();
    x = $("<p>").html(unescape(x.replace(/\u([\d\w]{4})/gi, function (match, grp) {
        return String.fromCharCode(parseInt(grp, 16));
    }))).text().replace(/\\/g, "");
    alert(x);
});

DEMO

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.