In the code provided below, you will see a map made of various tiles. Clicking on a tile will “select” it.
What I want to happen:
- Click on a tile
- Press the “Place” button
- An oil derrick appears on that tile
- Press the “Remove” button
- The oil derrick goes away
- Click another tile
- Press the “Place” button
- A new oil derrick appears on that tile
The program works up until steps 7-8. For some reason, the oil derrick isn’t appearing again.
I have the Place and Remove buttons set up so that only one oil derrick may be on the map at a time. To remove an oil derrick, I do not expect the tile with the oil derrick on it to be selected before removal (hence, you can have a different tile selected and clicking Remove will still remove the oil derrick no matter what tile it is on).
What I think is the problem is that the oil derrick isn’t getting appended correctly the second time. This may be because of the way it’s being removed.
http://jsfiddle.net/briz/jdhPW/17/
On a side not,
I realize that there is a potential bug if the Place button gets pressed before a tile is selected.
There are many ways to fix this. I think the simplest would be to $(‘.oilDerrick’).hide() rather than remove(). An alternative would be to append clones and change class names, that requires changing a bit more code though.
UPDATE: http://jsfiddle.net/jdhPW/19/