1

Why do I not get 2 images next to each other in my table row using the code below? You can copy/paste to JSFiddle

  var existingbody = document.getElementById('PulseBody');
     var newBody = document.createElement('tbody');
     var row = document.createElement('tr');

     var greenLight = document.createElement("img");
     greenLight.src = "http://placehold.it/50x50";
     greenLight.style.height = "30px";
     greenLight.style.width = "30px";

     var cellImg = document.createElement('td');
     cellImg.appendChild(greenLight);
     row.appendChild(cellImg);  

     var cellImg2 = document.createElement('td');
     cellImg2.appendChild(greenLight);
     row.appendChild(cellImg2);

     newBody.appendChild(row);

     existingbody.innerHTML = newBody.innerHTML;
<div class="container-fluid" style="padding: 0px;height:100%">
  <span id="PulseTableDT" style="padding-top:5px;font-size:10px">Incitialising...</span>
  <table id="PulseTable" class="display2" style="height:100%">
    <tbody id="PulseBody" style="height:100%">
      <tbody>
  </table>
</div>
ManInMoon
  • 6,795
  • 15
  • 70
  • 133

1 Answers1

1

It seems like a kind of weird action.

I think 'img' element made of 'createElement' used only one time.

If you want a solution, how about using this.

Clone Node

var cellImg = document.createElement('td');
cellImg.appendChild(greenLight.cloneNode(true));   // used 'cloneNode' function
row.appendChild(cellImg); 

This might be able to solve this problem.

UPDATE

And I've got a link to explain this.

I hope this can help you. :)

https://stackoverflow.com/a/6245051/8481089

Canet Robern
  • 1,049
  • 2
  • 11
  • 28
  • Yes. I had come to a similar conclusion. cloneNode() doesn't work for me,. However, if I create another element, then I get the two side-by-side. Your reference to the explanatory article was very helpful. Thank you. – ManInMoon Nov 22 '17 at 08:55
  • @ManInMoon I'm sorry for 'cloneNode()' did not work. But it's my pleasure I could help you. :) – Canet Robern Nov 23 '17 at 00:07