Skip to content

Instantly share code, notes, and snippets.

@tmtk75
Created April 28, 2013 02:10
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save tmtk75/5475559 to your computer and use it in GitHub Desktop.
<!DOCTYPE HTML>
<body>
<div id='f0' data-foo_0='{"age":37,"name":"ichiro"}'>foo_0</div>
<div id='f1' data-foo_1='{age:37,name:"ichiro"}'>foo_1</div>
<div id='f2' data-foo-2='{"age":37}'>foo-2</div>
<div id='f3' data-foo-bar='{"age":37}'>foo-bar</div>
<div id='f4' data-fiz-biz="{'age':37}">fiz-biz</div>
<div id='ichiro' data-profile='{"eyes":"black","age":37,"alive":true,"likes":["apple","orange"],"phones":{"iphone":"4S","android":"nexus7"}}'>ichiro</div>
<div id='jiro' data-profile='{"tall":172}' data-name='jiro'>jiro</div>
</body>
<!-- <script src='http://code.jquery.com/jquery-1.8.3.min.js'></script> -->
<!-- <script src='http://code.jquery.com/jquery-1.9.0.min.js'></script> -->
<script src='http://code.jquery.com/jquery-2.0.0.min.js'></script>
<script type="text/javascript">
var show = function($node, attrname, datakey) {
var attrval = $node.attr(attrname)
var dataval = $node.data(datakey)
console.log(attrname, $node.data(), dataval, typeof dataval, attrval, typeof attrval)
}
show($(f0), "data-foo_0", "foo_0")
show($(f1), "data-foo_1", "foo_1")
show($(f2), "data-foo-2", "foo-2")
show($(f3), "data-foo-bar", "foo-bar")
show($(f4), "data-fiz-biz", "fiz-biz")
</script>
<script type="text/javascript">
var $ichiro = $(ichiro)
console.log("$ichiro.data()", $ichiro.data())
console.log('$ichiro.data("profile")', $ichiro.data("profile"))
console.log('$ichiro.data("profile.a")', $ichiro.data("profile.a")) // can return if 1.8.3
$ichiro.data("profile").tall = 174
console.log('$ichiro.data("profile").tall', $ichiro.data("profile").tall)
console.log('$ichiro.attr("data-profile")', $ichiro.attr("data-profile"))
</script>
<script type="text/javascript">
var $jiro = $(jiro)
console.log('$jiro.data("profile")', $jiro.data("profile"))
var a = $jiro.data("profile")
console.log(a === $jiro.data("profile"))
console.log(a === $(jiro).data("profile"))
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment