1

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?

1
  • Why do you need to test the class in the if statement? arr can only contain elements with that class, since that's how you selected them. Commented Mar 30, 2015 at 18:50

4 Answers 4

1

Because className contains multiple classes, this does not work:

arr[i].className === "selected"

Since arr at that point contains only elements that include the "selected" class, you don't need that check.

However, if you need it for a different reason, you can use classList.contains instead:

arr[i].classList.contains("selected")

Fiddle

Sign up to request clarification or add additional context in comments.

1 Comment

Nice catch! I didn't realize the check was useless.
0

Does your elements have more than one class? If yes, maybe this helps:

if(key === 69 && arr[i].className.indexOf("selected") > -1) {

Comments

0

Changing this line with indexOf() works:

if(key === 69 && arr[i].className.indexOf("selected")>-1) 

Updated JS Fiddle

That's because the className is not equal to selected, but to selected box center green size200.

Comments

0

I would add the function for has class that way you don't have to worry about if you have a class that contains selected and then just add the combination of the key code and hasclass().

Here's the function for hasClass and how you can modify your code:

function hasClass(el, cls) {
  return el.className && new RegExp("(\\s|^)" + cls + "(\\s|$)").test(el.className);
}

Then just change your code to be:

if(key === 69 && hasClass(arr[i],"selected")) {
                box1.classList.toggle('circle');
            }   

Here is the updated fiddle JSFiddle

1 Comment

Good point about the possibility of a "...selected..." class. However, since OP is already using classList, the classList.contains method should do the work of hasClass.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.