I think a pretty good start would be to add a +
there.
'<div id="id-' my_id_var '"></div>'
Just isn't going to work.
"<div id=\"id-" + getNextID() + "\"></div>"
Will work just fine, though.
Second issue, each element gets 1 ID.
The point is that an ID is given to make an element uniquely-identifiable.
Why do you want the element to be uniquely identifiable twice?
There are lots of other ways of doing this.
First, you have class
.
If you are working with objects which need similar states ("selected", "news-article", "user-interactive"), use classes.
Then, you have data-
attributes.
<div id="player_1"
class="player-character"
data-health="42"
data-x="320"
data-y="240"
data-speed="5"
><img src="..."
></div>
Then in JS, you could do something like:
var player_1 = document.getElementById("player_1");
player_1.dataset.health; // "42"
These values are supported as far back as IE8, I think.
If you want to go further than that, then instead of using .dataset
, you use .setAttribute("data-" + field_name, value)
If you're using jQuery, then it handles ALL of that for you, anyway.
So with all of these options, why do you want 2 ids per one element?