0 points

Element.insertAdjacentElement() - beforebegin - afterend - beforeend - afterbegin - insert node element


dashboard

Fri Oct 23 2020 13:41:09 GMT+0000 (UTC)

Posted by @julien #javascript #html

<!-- beforebegin -->
<p>
  <!-- afterbegin -->
  foo
  <!-- beforeend -->
</p>
<!-- afterend -->

beforeBtn.addEventListener('click', function() {
  var tempDiv = document.createElement('div');
  tempDiv.style.backgroundColor = randomColor();
  if (activeElem) {
    activeElem.insertAdjacentElement('beforebegin', tempDiv);
  }
  setListener(tempDiv);
});

afterBtn.addEventListener('click', function() {
  var tempDiv = document.createElement('div');
  tempDiv.style.backgroundColor = randomColor();
  if (activeElem) {
    activeElem.insertAdjacentElement('afterend', tempDiv);
  }
  setListener(tempDiv);
});
content_copy Copy

https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentElement