mercredi 6 juillet 2016

get data-attributes with jQuery containing "

When getting data-Attributes with jQuery's .data() and this data-attribute contains the HTML-Entitys for double quotes, it fails to convert it to a JSON-Object. As you see below, it's simply seen as no valid JSON and therefore treats it like a string:

var dataAsString = $("[data-someprop]").data("someprop");

var dataAsJSON = $("[data-anotherprop]").data("anotherprop");

$(".target1").append(typeof(dataAsString));
$(".target2").append(typeof(dataAsJSON));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div data-someprop='{
                    "key": "some &quot;value&quot; in quotes"
                    }'></div>

<div data-anotherprop='{
                    "key": "some value without quotes"
                    }'></div>


<div class="target1"></div>
<div class="target2"></div>

How may i pass double quotes through JSON-Objects in data-attributes?

Aucun commentaire:

Enregistrer un commentaire