Jump to content

how to select all data attributes using jquery each function

Recommended Posts

hi all! after long time i'm back here with a problem.

i want to select all data attributes from a div with its name and value using jquery each function use that elsewhere.

here is the html for the div.

<div class='EW' data-rowid='1' data-unit='' data-workdesc='description' data-vby='2' data-distanceTot='50' data-AVcutTot='500' data-VcutTot='25000'></div>

and i tested the following to get my result.

		var datas = $(".EW").attributes;
		$.each(datas, function(i,item) {
			$("#msg").text(item.name + ":" + item.value);

but i could not get any result. can u please guide me what is wrong? or suggest me any other best method to achieve this.


thank you in advance.

Share this post

Link to post
Share on other sites

All the data-* attributes should be lowercase, that is why the code on running corrects them

$(function() {
                var datas = $(".EW").data();

                var storcurrent = "";
                $.each(datas, function(indexname, value) {
                    storcurrent += "data-" + indexname + ":" + value + "; ";




Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now