0

I'm trying to add an dynamically created element ( with document.createElement('span') ) inside an dynamically created div.

This is my code:

// Newplayer div holder
var newPlayer = document.createElement('div');
newPlayer.setAttribute('id', 'player_' + playerID);
newPlayer.setAttribute('class', 'player_entry');
newPlayer.setAttribute('style', 'background-color: ' + getRandomColor());

// Points holder
var playerTotalPointsHolder = document.createElement('div');
playerTotalPointsHolder.setAttribute('id', 'player_' + playerID + "_total_holder");
playerTotalPointsHolder.setAttribute('class', 'player_total_holder');

var playerTotalPoints = document.createElement('span');
playerTotalPoints.setAttribute('id', 'player_' + playerID + "_total");
playerTotalPoints.setAttribute('class', 'player_total');
playerTotalPoints.innerHTML = 0;

// Put the pieces together
playerTotalPointsHolder.innerHTML = "Total: " + playerTotalPoints;

But the output is:

Playername [object HTMLDivElement]

So my question is, how can i insert the element inside the element?

4 Answers 4

1
playerTotalPointsHolder.innerHTML = "Total: ";
playerTotalPointsHolder.appendChild(playerTotalPoints);
Sign up to request clarification or add additional context in comments.

Comments

0

Try changing this line:

playerTotalPointsHolder.innerHTML = "Total: " + playerTotalPoints;

to this:

playerTotalPointsHolder.appendChild(
  document.createTextNode('Total: ')
);
playerTotalPointsHolder.appendChild(
  playerTotalPoints
);

4 Comments

That did the trick, thanks. But what's the effort of creating an textnode instead of just inserting an string?
@Mathlight It's more consistent, and it doesn't destroy sibling elements when they exist. There are no practical advantages for your specific case.
Alright, thanks for your explanation. I'll accept your answer because of the consistent part.
@Mathlight I agree with sabof, it is usually discouraged to change innnerHTML directly (see for example "Security considerations" here)
0

You need to use appendChild for this:

// Setting total score...
playerTotalPoints.innerHTML = "Total: " + 0;

// And pushing `span` to `div`
playerTotalPointsHolder.appendChild(playerTotalPoints);

With playerID 1, this will result in the following HTML:

<div id="player_1_total_holder" class="player_total_holder">
   <span id="player_1_total" class="player_total">Total: 0</span>
</div>

However, I suggest that instead of using span you should use p as paragraph for text. But even then, same principle applies for pushing element inside an element.

Comments

0

You created your elements but you didn't append them anywhere in your DOM. You need, at the end of your code, do something like this:

playerTotalPointsHolder.appendChild(playerTotalPoints);
newPlayer.appendChild(playerTotalPointsHolder);
someDOMElementInYourDocument.appendChild(newPlayer);

1 Comment

please provide some description

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.