Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get the value of html5 <data-something="value"> with jquery

Tags:

html

jquery

The other day I was reading the answers here in stackoverflow and I read, "jquery can get the value of the html5 <data-something='value'>", I read this when I was looking for something else so I just read and don't analyse this. And now I really need accomplish this task.

The question is: is it posible or I just misunderstand the reading?

like image 246
figuedmundo Avatar asked Aug 24 '12 05:08

figuedmundo


2 Answers

<div id="test" data-key="abcd">hahahah</div>

<script type="text/javascript">
var value = $("#test").data("key");
alert(value); //This will show you "abcd"
</script>

I think data isn't only for HTML5 but also in HTML4

like image 76
Patrick Jeon Avatar answered Nov 09 '22 02:11

Patrick Jeon


You can use data() and have jQuery attempt to decode the type and return the typed value.

If you want the raw string, use attr("data-x").

like image 39
alex Avatar answered Nov 09 '22 02:11

alex