It is possible to expands an object literal as html5 data- attributes?
Having the following object:
const requirements = {
'data-description': 'some text...',
'data-pointer': true,
'data-speaker': true
}
I would like to expand it in a anchor tag in order to get something like this:
<a href="#" class="show-modal" data-description="some-text" data-pointer="true" data-speaker="true">Show modal</a>
I tried to use the spread syntax in this way <a href="#" class="show-modal" `${...requirements}`>Show modal</a>
But nothing get printed
I am now depending on this function that builds an anchor and passes the data dynamically.
function buildAnchor(requirements) {
const anchor = document.createElement('a');
anchor.setAttribute('class', 'show-modal');
anchor.setAttribute('href', '#');
anchor.textContent = 'More info';
Object.keys(requirements).forEach(data => {
anchor.setAttribute(data, requirements[data]);
});
return anchor.outerHTML;
}
This function do the job, but i would like to know if it's possible to use spread syntax
Thanks in advance