1var my_elem = document.getElementById('my_id');
2
3var span = document.createElement('span');
4 span.innerHTML = '*';
5 span.className = 'asterisk';
6
7my_elem.parentNode.insertBefore(span, my_elem);
1// The Node.insertBefore() method inserts a node before a
2// reference node as a child of a specified parent node.
3let insertedNode = parentNode.insertBefore(newNode, referenceNode)
1// create alert div
2const alert_box = document.createElement("div")
3alert_box.innerText = "Not allowed!";
4alert_box.className = "alert";
5
6//get the parrent of where u wanna insert
7const cont = document.querySelector(".container");
8// get the element you wanna insert before
9const prof = document.getElementById("profile");
10// do the insert
11cont.insertBefore(alert_box, prof);
1// add without creating a new element
2document.getElementById('my_id')
3.insertAdjacentHTML('beforebegin', 'your_html_code');