I have several elements which have I would like to add a class to their pseudo :before element when they are clicked on. Currently I have the class added to the elements, but am having trouble figuring out how to attach the listener to to the element and add the class to the pseudo-element.
Here is my HTML:
<div class="locked-item"></div>
SCSS:
.locked-item{
&:before {
content: '';
background-image: url(../../images/lock.svg);
background-position: center center;
background-size: cover;
background-size: 37%;
background-repeat: no-repeat;
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
}
background-color: @brand-green;
transition: all .2s;
width: 100%;
height: 100%;
position: absolute;
top: 100%;
left: 0;
}
Javascript:
setTimeout(function() {
var lockItems = document.getElementsByClassName('locked-item');
for (var i = 0; i < lockItems.length; i++) {
lockItems[i].addEventListener('click',handleClick,false);
}
}, 100);
function handleClick(){
addClass(this, 'jiggle-click');
setTimeout(function() {
removeClass(this, 'jiggle-click');
}.bind(this), 820);
}
function hasClass(el, className) {
if (el.classList)
return el.classList.contains(className)
else
return !!el.className.match(new RegExp('(\\s|^)' + className + '(\\s|$)'))
}
function addClass(el, className) {
if (el.classList)
el.classList.add(className)
else if (!hasClass(el, className)) el.className += " " + className
}
function removeClass(el, className) {
if (el.classList)
el.classList.remove(className)
else if (hasClass(el, className)) {
var reg = new RegExp('(\\s|^)' + className + '(\\s|$)')
el.className=el.className.replace(reg, ' ')
}
}