So I'm making a function to select a clicked span element, depending on its data attribute and its value. I made this
function moveFilterElements(event) {
if ($(event).hasClass('active')) {
var dataAttributes = $(event).data(),
dataKey = Object.keys(dataAttributes)[0],
dataValue = dataAttributes[Object.keys(dataAttributes)[0]];
$(event).parents('.filters-container').find('.label[data-' + dataKey + '=' + dataValue + ']').toggleClass('active');
$(event).parents('.filters-container').find('.btn-show-filters').find('.label[data-' + dataKey + '=' + dataValue + ']').remove();
updateFiltering();
}
else {
var clonedActiveItem = $(event).clone(true);
$(event).parents('.filters-container').find('.filters-selected').append(clonedActiveItem);
clonedActiveItem.toggleClass('active');
$(event).toggleClass('active');
updateFiltering();
}
}
F.ex data attributes looks like this: 'data-subject' - works fine.
'data-category-name' converts to 'categoryName' due to the HTML DOM standard.
I could bypass this with just naming them 'data-categoryname' but that also conflicts with the naming standard.
So how do I get around this? Do I need a regex to pick apart the word again? Is that the only way?