Quoting through element data attributes

Per,

How to iterate over all attributes in an HTML element?

you will get a general solution:

for (var i = 0; i < elem.attributes.length; i++) { var attrib = elem.attributes[i]; if (attrib.specified) { alert(attrib.name + " = " + attrib.value); } } 

How would I do this a general decision more specific as to prevent only the value of the attribute data.

Do I need a regex attrib.name or is there a simpler way? Here are a few examples of HTML data with two attributes:

 <div id='universals' data-path='/zz/' data-load='1'></div> 
+4
source share
5 answers

In many modern browsers, we have access to these special attributes through the penis .dataset on site Node . Unfortunately, this is not an accepted standard, and therefore we do not see that it is accepted in the whole spectrum. Fortunately, in every major browser has partial support in the fact that these attributes can be accessed using common techniques such as getAttribute , and by cycle through the list .attributes .

The code below shows the second method:

 // Reference to our element var element = document.getElementById("universals"), attr; // Cycle over each attribute on the element for (var i = 0; i < element.attributes.length; i++) { // Store reference to current attr attr = element.attributes[i]; // If attribute nodeName starts with 'data-' if (/^data-/.test(attr.nodeName)) { // Log its name (minus the 'data-' part), and its value console.log( "Key: " + attr.nodeName.replace(/^data-/, ''), "Val: " + attr.nodeValue ); } } data-' part), and its value // Reference to our element var element = document.getElementById("universals"), attr; // Cycle over each attribute on the element for (var i = 0; i < element.attributes.length; i++) { // Store reference to current attr attr = element.attributes[i]; // If attribute nodeName starts with 'data-' if (/^data-/.test(attr.nodeName)) { // Log its name (minus the 'data-' part), and its value console.log( "Key: " + attr.nodeName.replace(/^data-/, ''), "Val: " + attr.nodeValue ); } } ^ data- /, ''), // Reference to our element var element = document.getElementById("universals"), attr; // Cycle over each attribute on the element for (var i = 0; i < element.attributes.length; i++) { // Store reference to current attr attr = element.attributes[i]; // If attribute nodeName starts with 'data-' if (/^data-/.test(attr.nodeName)) { // Log its name (minus the 'data-' part), and its value console.log( "Key: " + attr.nodeName.replace(/^data-/, ''), "Val: " + attr.nodeValue ); } } 

Fiddle: http://jsfiddle.net/pGGqf/14/

You should find that this approach will work in all major browsers, even IE6. It optionally, again, in browsers that support element .dataset . The object .dataset have a bit more functionality, so you can detect them if you want to:

 if (element.dataset) { // Browser supports dataset member } else { // Browser does not support dataset member } 
+4
source

dataset support is very good, if you do not need IE below version 11

Simple iteration "in-in" in the property data set:

 var data = document.querySelector('div').dataset; for( var i in data ) console.log(i, data[i]) ') dataset; var data = document.querySelector('div').dataset; for( var i in data ) console.log(i, data[i]) 
 <div data-foo='1' data-bar='2'></div> 
Run codeHide result
+2
source

If you do not want to use regex, you can try

 if attrib.name.startswith('data'): //do something 
+1
source

I have a concept that may work for you.

 var el = document.getElementById("universals"); for(var i=0;i<el.attributes.length;i++){ if((el.attributes[i].nodeName+"").indexOf("data-")>-1){ var key=(el.attributes[i].nodeName+"").substring(5); var value=el.attributes[i].value; alert(key+": "+value); } } ); var el = document.getElementById("universals"); for(var i=0;i<el.attributes.length;i++){ if((el.attributes[i].nodeName+"").indexOf("data-")>-1){ var key=(el.attributes[i].nodeName+"").substring(5); var value=el.attributes[i].value; alert(key+": "+value); } } { var el = document.getElementById("universals"); for(var i=0;i<el.attributes.length;i++){ if((el.attributes[i].nodeName+"").indexOf("data-")>-1){ var key=(el.attributes[i].nodeName+"").substring(5); var value=el.attributes[i].value; alert(key+": "+value); } } .nodeName + "") substring ( var el = document.getElementById("universals"); for(var i=0;i<el.attributes.length;i++){ if((el.attributes[i].nodeName+"").indexOf("data-")>-1){ var key=(el.attributes[i].nodeName+"").substring(5); var value=el.attributes[i].value; alert(key+": "+value); } } ; var el = document.getElementById("universals"); for(var i=0;i<el.attributes.length;i++){ if((el.attributes[i].nodeName+"").indexOf("data-")>-1){ var key=(el.attributes[i].nodeName+"").substring(5); var value=el.attributes[i].value; alert(key+": "+value); } } value); var el = document.getElementById("universals"); for(var i=0;i<el.attributes.length;i++){ if((el.attributes[i].nodeName+"").indexOf("data-")>-1){ var key=(el.attributes[i].nodeName+"").substring(5); var value=el.attributes[i].value; alert(key+": "+value); } } 

This is the main idea that I came across that works very well. I also created a function that returns an Object Name-Value-Pair attribute "data" HTMLElements, using the method above.

 function data2Obj(id){ var obj={}; var el=document.getElementById(id); for(var i=0;i<el.attributes.length;i++){ if((el.attributes[i].nodeName+"").indexOf("data-")>-1){ var key=(el.attributes[i].nodeName+"").substring(5); var value=el.attributes[i].value; if(value.toLowerCase()=="true")value=true; else if(value.toLowerCase()=="false")value=false; else if((parseInt(value)+"")==value)value=parseInt(value); obj[key]=value; } } return obj; } { function data2Obj(id){ var obj={}; var el=document.getElementById(id); for(var i=0;i<el.attributes.length;i++){ if((el.attributes[i].nodeName+"").indexOf("data-")>-1){ var key=(el.attributes[i].nodeName+"").substring(5); var value=el.attributes[i].value; if(value.toLowerCase()=="true")value=true; else if(value.toLowerCase()=="false")value=false; else if((parseInt(value)+"")==value)value=parseInt(value); obj[key]=value; } } return obj; } + "") indexOf (. "data -")> - function data2Obj(id){ var obj={}; var el=document.getElementById(id); for(var i=0;i<el.attributes.length;i++){ if((el.attributes[i].nodeName+"").indexOf("data-")>-1){ var key=(el.attributes[i].nodeName+"").substring(5); var value=el.attributes[i].value; if(value.toLowerCase()=="true")value=true; else if(value.toLowerCase()=="false")value=false; else if((parseInt(value)+"")==value)value=parseInt(value); obj[key]=value; } } return obj; } ; function data2Obj(id){ var obj={}; var el=document.getElementById(id); for(var i=0;i<el.attributes.length;i++){ if((el.attributes[i].nodeName+"").indexOf("data-")>-1){ var key=(el.attributes[i].nodeName+"").substring(5); var value=el.attributes[i].value; if(value.toLowerCase()=="true")value=true; else if(value.toLowerCase()=="false")value=false; else if((parseInt(value)+"")==value)value=parseInt(value); obj[key]=value; } } return obj; } 

This can be easily changed to accept a class or any other method for selecting HTMLElements.

If you take a return of the function and iterate through the object, you have to get the desired result.

 var datas = data2Obj("universal"); for(var k in datas){ alert(k+": "+datas[k]); } ); var datas = data2Obj("universal"); for(var k in datas){ alert(k+": "+datas[k]); } datas [k]); var datas = data2Obj("universal"); for(var k in datas){ alert(k+": "+datas[k]); } 
0
source
 _dataToObject = function( dataset ) { return Object.keys( dataset ).reduce( function( object, key ) { object[ key ] = dataset[ key ]; return object; }, {}); } (object, key) { _dataToObject = function( dataset ) { return Object.keys( dataset ).reduce( function( object, key ) { object[ key ] = dataset[ key ]; return object; }, {}); } ]; _dataToObject = function( dataset ) { return Object.keys( dataset ).reduce( function( object, key ) { object[ key ] = dataset[ key ]; return object; }, {}); } 
0
source

All Articles