Aymand Osanes Aymand Osanes - 1 year ago 213
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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download