I have attribute names stored somewhere in a string and need a way to create corrected names to use with dataset
api. I get the names of those in attribute style, like data-attrib-name
. So I need to convert them to camel case and remove the data-
in front of it.
The shortest way I found out for now is my replace below. But this feel kinda hacky to me. Is there a better solution / shorter way for my task?
console.log(
"data-my-test-name"
.replace('data-', '')
.replace(/-([a-z]?)/g, (m, g) => g.toUpperCase())
);
element.dataset.myTestName = "test"
Seen as this is built into thedataset
property, why do you need to do this?. – Marybelleelement.setAttribute("data-my-test-name", "thevalue")
console.log(element.dataset.myTestName)
===thevalue
.. – Marybelle