html "data-" attribute as javascript parameter -
lets have this:
<div data-uid="aaa" data-name="bbb", data-value="ccc" onclick="fun(this.data.uid, this.data-name, this.data-value)"> and this:
function fun(one, two, three) { //some code } well not working have absolutely no idea why. post working example please?
the easiest way data-* attributes element.getattribute():
onclick="fun(this.getattribute('data-uid'), this.getattribute('data-name'), this.getattribute('data-value'));" demo: http://jsfiddle.net/pm6ch/
although suggest passing this fun(), , getting 3 attributes inside fun function:
onclick="fun(this);" and then:
function fun(obj) { var 1 = obj.getattribute('data-uid'), 2 = obj.getattribute('data-name'), 3 = obj.getattribute('data-value'); } demo: http://jsfiddle.net/pm6ch/1/
the new way access them property dataset, isn't supported browsers. you'd them following:
this.dataset.uid // , this.dataset.name // , this.dataset.value demo: http://jsfiddle.net/pm6ch/2/
also note in html, there shouldn't comma here:
data-name="bbb", references:
element.getattribute(): https://developer.mozilla.org/en-us/docs/dom/element.getattribute.dataset: https://developer.mozilla.org/en-us/docs/dom/element.dataset.datasetbrowser compatibility: http://caniuse.com/dataset
Comments
Post a Comment