S
S
Shimpanze2018-02-13 09:10:32
JavaScript
Shimpanze, 2018-02-13 09:10:32

JavaScript: Why doesn't insertAdjacentHTML - afterEnd work?

Good afternoon!
Looping through the elements of an array:

tempImages.forEach(function(item, i, arr) {
  var img = new Image();

  // здесь собираю нужный мне img

  // далее, мне необходимо обернуть элемент item (объект изображения) в ссылку
  item.insertAdjacentHTML('beforeBegin', '<a href="' + href + '">');
  item.outerHTML.insertAdjacentHTML('afterEnd', '</a>');
});

As a result, I get the following structure:
<a href="..."></a>
<img />

And you need to get this:
<a href="...">
  <img />
</a>

Why doesn't this construct work?
item.outerHTML.insertAdjacentHTML('afterEnd', '</a>');

How to add a closing tag </a>- after the current element?
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Melnikov, 2018-02-13
@mlnkv

var anchor = document.createElement("a");
anchor.href = href;
anchor.appendChild(item);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question