Reputation: 67
Is it safe to use tile to identify the newly created div here:
var tile = document.createElement("div");
document.getElementById('tileBlock').appendChild(tile);
var tileName = 'tile' + numbersToLetters(tileX) + numbersToLetters(tileY);
tile.setAttribute('id', tileName);
tile = blah blah blah...
Or do I need to reattach tile?
var tile = document.createElement("div");
document.getElementById('tileBlock').appendChild(tile);
var tileName = 'tile' + numbersToLetters(tileX) + numbersToLetters(tileY);
tile.setAttribute('id', tileName);
tile = document.getElementById(tileName);
tile = blah blah blah...
Upvotes: 2
Views: 103
Reputation: 413818
You don't need to re-fetch the element, and you don't really need to use .setAttribute()
:
tile.id = tileName;
Changing the "id" property does not "break" your reference to the element. The value of the variable "tile" remains unchanged, in other words.
Upvotes: 3