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.

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 + "; ";




Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...