'#monsters[0]'
can't be parsed by jQuery as meaning the element has an id "monsters[0]"
. It's parsed as a query for an element having as id monsters
and an attribute 0
.
You may do
$(document.getElementById('monsters[0]')).animate( { left: "+=25" },500);
If you use it more than once you can build yourself a small utility :
function $$(id) { return $(document.getElementById(id)) }
so that you only have to do
$$('monsters[0]').animate( { left: "+=25" },500);
But you should probably avoid those id, they would for example bring you the same problem in CSS. Here it looks like a class or a data attribute would make more sense.
Comment :
There's of course the other solution of dynamically escaping the [
and ]
characters (and later on the other ones that could arise). Don't do it : this makes the code much more complex, hides the fact that it works only for certain forms of id, and is much heavier : you build a string that jQuery will parse to understand you need the id and then jQuery will call document.getElementById
. Direcly use document.getElementById
to be clearer, more reliable and more efficient.