0

I try to append a button on a div element, that isn't defined in the html page.

This is my Function to create the Button:

const img2dView = function (name) {
      return `<button class="gridButton">
            <img alt = ${name} src="$...images/${name}.png" style="width: 100%;">
          </button>`;
    };

if i append this on an already defined div it works:

$someDiv.append(img2dView(SomeName));

but if i try this on a new created div, the button is just a string:

let helpDiv = document.createElement('div');
      helpDiv.class = 'square';
      helpDiv.append(img2dView(SomeNames[i]));

Output:

<div>
     "<button class="gridButton">
            <img alt = blocked src="assets/.....png" style="width: 100%;">
          </button>;"
</div>

I think i need .appendChild. But then I get a error Message, that "img2dView(SomeNames[i])" isn't a node

psclp
  • 55
  • 7

2 Answers2

0

If you are using jQuery use as:

$someDiv = $('<div />');
$someDiv.append('<button class="gridButton"> <img alt = blocked src="assets/.....png" style="width: 100%;"> </button>');
Ashmah
  • 842
  • 2
  • 8
  • 17
0

It worked with:

const img2dView = function (name) {
      let div = document.createElement('div');
      div.innerHTML =`<button class="gridButton">
            <img alt = ${name} src="...${name}.png" style="width: 100%;">
          </button>`;
      return div.firstChild;
    };

thanks to: Creating a new DOM element from an HTML string using built-in DOM methods or Prototype

psclp
  • 55
  • 7