Search code examples
javascriptcustom-data-attributestring-length

How can i geht the lenght of all data-attributes of an specific elements?


HTML:

<div id="me" data-property-1="1" data-property-2="2" data-property-3="3"></div>

Is it possible to get the lenght of all data-attributes of the div with vanilla.js?


Solution

  • Can you try this:

    const dataSetLength = Object.keys(document.getElementById('me').dataset).length;
    console.log('Length of dataset:', dataSetLength);
    <div id="me" data-property-1="1" data-property-2="2" data-property-3="3"></div>