Show with an example how to replace the div with a page:

<div id="new_message"></div> 

on

 <div>Некий текст</div> <div id="new_message"></div> 

preferably with js.

  • one
    and where is the replacement of the div? I see the addition of a new diva with text before div with an identifier. Or I do not understand something? - Alexey Shimansky
  • It looks like that adding ... but it is necessary to replace the first diva with the other 2 divas. - Yuri
  • it is not clear from the ghost example - Alexey Shimansky

1 answer 1

Create a new div , add text to it, find the parent of the new_message block, and add the created div before the div with id="new_message"

 var el = document.getElementById('new_message'); var parentEl = el.parentNode; newEl = document.createElement('div'); newEl.innerHTML = 'Некий текст'; parentEl.insertBefore(newEl, el); 
  • How to add to the created div class? - Yuri
  • @Yuri, newEl.setAttribute ("class", "your_class_name"); - Klym