Aymand Osanes Aymand Osanes - 1 month ago 18
HTML Question

Insert HTML before element in JavaScript without jQuery

How can I rewrite this jQuery operation in pure JavaScript:

$("#my_id").before('<span class="asterisk">*</span>');

Answer

This will do what you want, without needing the support of any bloated libraries like jQuery.

var my_elem = document.getElementById('my_id');

var span = document.createElement('span');
    span.innerHTML = '*';
    span.className = 'asterisk';

my_id.parentNode.insertBefore(span, my_elem);