Cosmetic question: I have a html element containing possible dimensions for some embedded images, these are stored as:
<div class="inside" data-dimensions='{ "s-x": 213, "s-y": 160, "m-x": ...
I get out the data-dimension and parse with jQuery.parseJSON(jQuery.data("dimensions")) all fine and closely following the jquery's doc.
However I'm used to encapsulate all my html attributes inside double quotes:
<div class="inside" data-dimensions="{ 's-x': 213, 's-y': 160, 'm-x': ...
But then I get a malformed json exception. Are there ways so i can obey my self imposed "double quoted html attributes" law?
You can use " instead of ". But quoting orgies are horrible (in HTML even more than in PHP) so better go with single-quoting your html attributes.
BTW, you do not need to use .parseJSON - jQuery does that automatically if the data- attribute starts with { (actually, it's more complex - here's the regex it uses to test if it should be parsed as JSON: ^(?:\{.*\}|\[.*\])$).
Try this and you can keep nicely formatted JSON in the attribute:
$.parseJSON($('.inside').data('dimensions').replace(/'/g,"\""))
The JSON specification stipulates that keys and (string) values be quoted with double-quotes.
HTML attributes can be enclosed in either single or double quotes.
Personally, I wouldn't fight it and just go with what causes the least amount of friction and is easiest for all to understand which in this case is to single quote the HTML attributes and use double-quotes inside the attribute value.