show element's "data-xxx" attribute compared with its `dataset` property.
Last updated 2 years ago
Was this helpful?
⟩ ⟩ ⟩ ⟩
// ⭐️ elem.showDataAttr('data-xxx') // show element's "data-xxx" attribute & property Element.prototype.showDataAttr = function (dataAttr){ const prop = dataAttr.toDatasetPropName(); log(`${this.nodeName.toLowerCase()}.${prop} = '${this.dataset[prop]}'`); log(`${dataAttr}='${this.attr(dataAttr)}'`); } // sample result: // ----------------------- // div.widgetName = 'menu' // data-widget-name='menu'
String ⟩ - "data-xxx" attribute name to "dataset" property name.
Element ⟩ - get element's attribute
codepen ⟩