0

I am trying to run a while loop to remove all the elements of a particular class name from the DOM as given in the answer here. For some reason, I am getting an error.

Uncaught SyntaxError: Invalid or unexpected token

The code I am trying to run is lengthy but the basic idea is identical to the above-linked answer.

function resetGame() {
    var cardColumns = document.getElementsByClassName('card-column'); 

    while (cardColumns[0]) {
        cardColumns[0].parentNode.removeChild(cardColumns[0]);
    }
}

When I hover over the error (which shows at the end of the loops curly brace) it shows \u200b in Chrome Inspect.

I have tried changing the class name, and even directly copy-pasting the guy above answer. Same error. When I paste the loop into the console on its own I get the same thing, so my best assumption is I have something incorrect fundamentally with the syntax, not the rest of my code.

enter image description here

Any ideas?

yohohosilver
  • 367
  • 1
  • 2
  • 15
  • 1
    There's nothing wrong with the syntax in the code shown. Have you searched your source JS file to see if it has any \u200b characters in it? EDIT: Check the end of line 255. – nnnnnn Oct 23 '17 at 02:59
  • I'm not really sure how to check to be honest – yohohosilver Oct 23 '17 at 03:01
  • 1
    Just remove one of `}` and call the function, everything okay. – ferhado Oct 23 '17 at 03:01
  • 1
    You should be able to delete that character (it's a [zero-width space](https://en.wikipedia.org/wiki/Zero-width_space)) in any decent editor. – Ray Toal Oct 23 '17 at 03:01
  • 1
    Well \u200b is a zero-width space character, so you can't really see it although some editors will show a place-holder dot for that sort of whitespace character. But if you place the cursor on that line and press the right arrow you should note that it takes one more press than you'd expect before it moves the cursor to the next line. (Or you could just delete that line and retype it.) – nnnnnn Oct 23 '17 at 03:02
  • Weird. So I deleted the bracket and retyped it and now it works... WTH? Thank you, never seen this before. – yohohosilver Oct 23 '17 at 03:02
  • 1
    That's actually expected behavior. Non-printing characters get into your source code when you copy-paste. Happens a lot. You can't see them, but you can delete them. – Ray Toal Oct 23 '17 at 03:03
  • Appreciate the knowledge! I'll just leave this here for other noobs like me. – yohohosilver Oct 23 '17 at 03:05

0 Answers0