Skip to content

Instantly share code, notes, and snippets.

@jiglesiasabio
Forked from charliepark/gist:4266921
Last active August 29, 2015 14:02
Show Gist options
  • Save jiglesiasabio/c170868b80de9551bd68 to your computer and use it in GitHub Desktop.
Save jiglesiasabio/c170868b80de9551bd68 to your computer and use it in GitHub Desktop.
Using HTML5 data attributes to store arrays
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
</head>
<body>
<div id="animals" data-animals='["cat", "dog", "bird"]'></div>
<div id="vehicles" data-vehicles='{"motorcycle":"Harley", "car":"Herbie", "steamshovel":"Mike"}'></div>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" charset="utf-8">
$(function(){
var a = JSON.parse($('#animals').attr('data-animals'))[0];
$('#animals').html(a);
var v = JSON.parse($('#vehicles').attr('data-vehicles')).car;
$('#vehicles').html(v);
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment