Struggling with getting a click event to function properly on my webpage. I have a Div labeled 'icon' with the class 'lock', and I aim to allow users to click on the background image of this Div to switch its class from 'lock' to 'locked'.
To clarify, both 'lock' and 'locked' classes are defined in an external CSS file and include background images for the Div. Additionally, I am steering clear of JQuery and instead opting for addEventListener paired with a custom function. As of now, here is a snippet of my JS code:
var elLock = document.getElementById('icon');
function changeLock(){
var imgSwitch = elLock.getAttribute('class');
if(imgSwitch !== 'unlock'){
elLock.className = 'unlock';
}else{
elLock.className('lock');
}
}
elLock.addEventListener('click', changeLock, false);
The end goal can be visualized in this informative youtube video: https://www.youtube.com/watch?v=oI2sRCN7CiM
Your assistance would be highly valued as I aspire to learn and grow from any errors made.