Bookmarklet doesn’t run

I’m new to Javascript, and this bookmarklet I’m trying to make seems to not work. If I remove this one piece of code in the script, everything works.
javascript:(function() {var elems = document.getElementsByTagName('div')for (var i = 0; i<elems.length; i++) { elems[i].style.backgroundColor = 'black';} })();

I tried putting the code into the bookmarklet and using it. Instead of setting all the divs’ background colors to black, nothing happens.