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:


Comments

Popular posts from this blog

Change php variable from jquery value using ajax (same page) -

Pull out data related to my apps from Android Play Store and iOS App Store -

How can I fetch data from a web server in an android application? -