Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single quotes in data attribute containing json

Tags:

json

jquery

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?

like image 715
dr jerry Avatar asked Mar 02 '11 13:03

dr jerry


1 Answers

You can use &quot; 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: ^(?:\{.*\}|\[.*\])$).

like image 184
ThiefMaster Avatar answered Nov 11 '22 13:11

ThiefMaster