javascript - cant get data From data-n in element -


i have span :

<span class="span-link" id="2" data-state="true" onclick="javascript: changestate(this.id);">change state</span> 

and in javascript function use code value :

function changestate(_thingid) {     alert(_thingid);     var tt = $(this).attr("data-state");     alert(tt);         } 

i getting undefinded data-state . test $(this).data("state") , getting undefined .

whats problems?

you referencing id, not object, change this:

var tt = $('#'+_thingid).attr("data-state"); 

https://jsfiddle.net/6ne5b8zm/


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -