Hello, You can insert html inside the element elem.innerHTMl = html;

And how to insert it next? (before / after all the difference) Without creating an additional wrapper

Big thank you

2 answers 2

I threw an example using this function , although I was a little confused by her status of Working Draft :

 var elem = document.getElementById('elem'); function pasteAfterEnd() { elem.insertAdjacentHTML('afterend', '<p>Абзац 100</p>'); } function pasteBefore() { elem.insertAdjacentHTML('afterbegin', '<p>Абзац 0</p>'); } function pasteAfter() { elem.insertAdjacentHTML('beforeend', '<p>Абзац 3</p>'); } 
 #elem { background: #ccc; } 
 <div id="elem"> <p>Абзац 1</p> <p>Абзац 2</p> </div> <button onclick="pasteAfterEnd()">Вставить рядом</button> <button onclick="pasteBefore()">Вставить перед</button> <button onclick="pasteAfter()">Вставить после</button> 

  • here you insert it in the elem, and in the question you need a number - Grundy
  • added the "paste near" function :) - MasterAlex
  • @Grundy, 'beforebegin' and 'afterend' , and the method is the same. - Qwertiy
  • Let me kiss you !!!)))) Thank you very much, exactly what you need! - Stanislav Sagan

 var elem = document.getElementById('elem'), // поиск нужного элемента p = document.createElement('p'); // создание "вставляемого" элемента p.innerHTML = '0'; // наполнение его контентом elem.parentElement.insertBefore(p,elem); // добавление контента через родителя 
 <p>1</p> <p>2</p> <p id="elem">3</p> <p>4</p> <p>5</p>