Need some help figuring out what's wrong with my second function. JSfiddle here: JSfiddle
//add an outline on click
function addOutline(){
box1.classList.toggle('selected');
}
box1.addEventListener('click', addOutline, false);
//change shape on element with class "selected" when "e" is pressed
function changeShape (event) {
var arr = document.getElementsByClassName("selected");
var key = event.keyCode;
for(var i = 0; i < arr.length; i++) {
if(key === 69 && arr[i].className === "selected") {
box1.classList.toggle('circle');
}
}
}
document.addEventListener("keydown", changeShape, false);
Basically what i'm trying to do is, when the key "e" is pressed AND if the elements className is equal to "selected" then the div "box" should toggle the circle class (from the css).
I got it to work with only the keyDown, but when trying to add the className statement to the function, it wont work. Check the jsfiddle at the top to make more sense of this. Any ideas?
ifstatement?arrcan only contain elements with that class, since that's how you selected them.