Created
September 30, 2014 13:22
-
-
Save ethyde/ccba519419a062eb7ac7 to your computer and use it in GitHub Desktop.
Data-attr
http://stackoverflow.com/a/7262427 et http://devlint.fr/blog/html5-jquery-et-les-data-attributes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* 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