Aymand Osanes Aymand Osanes - 10 months ago 95
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 Source

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);