Skip to content

Instantly share code, notes, and snippets.

@ethyde
Created September 30, 2014 13:22
Show Gist options
  • Select an option

  • Save ethyde/ccba519419a062eb7ac7 to your computer and use it in GitHub Desktop.

Select an option

Save ethyde/ccba519419a062eb7ac7 to your computer and use it in GitHub Desktop.
<a id="foo"
href="#"
data-str="bar"
data-bool="true"
data-num="15"
data-json='{"id":"2", "fizz":["buzz", "pariatur", "esse", "cillum"], "lat":"48.861395", "lon":"2.35136", "title":"Paris", "number" : 12584, "bool": true}'
data-json2='{"id":"3", "fizz":[{"id": 0, "name": "Imelda Dalton"},{"id": 1,"name": "Kayla Padilla"}], "lat":"48.861395", "lon":"2.35136", "title":"Paris"}'>foo!</a>
/* jQuery */
$('#foo').data('str'); // string bar
$('#foo').data('bool'); // boolean true
$('#foo').data('num'); // number 15
$('#foo').data('json'); // object {"id":"2", "fizz":["buzz", "pariatur", "esse", "cillum"], "lat":"48.861395", "lon":"2.35136", "title":"Paris", "number" : 12584, "bool": true}
$('#foo').data('json').lat; // string 45.766242
$('#foo').data('json').number; // number 12584
$('#foo').data('json').bool; // boolean true
$('#foo').data('json').fizz; // object ["buzz", "pariatur", "esse", "cillum"]
$('#foo').data('json').fizz[1]; // string pariatur
$('#foo').data('json2').fizz; // [{ id=0, name="Imelda Dalton"},{ id=1, name="Kayla Padilla"}]
$('#foo').data('json2').fizz[0].name; // Imelda Dalton
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment