Jump to content

how to select all data attributes using jquery each function


funbinod

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

                })
                $("#msg").text(storcurrent);

            });

 

Link to comment
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
×
×
  • Create New...