before()

Insert elements before the target Element

Inserting an HTML string

$('#target').before('<p class="main">I am before! <strong>Very Strong </strong></p>');

//OUTPUT:
<p class="main">I am before! <strong>Very Strong </strong></p>
<div id="target"></div>

Inserting an element .

const newDiv = document.createElement('div');
newDiv.textContent = 'I am before!';
$('section > div*').before(newDiv));

Insert Multiple elements.

const elements = [
   document.createElement('span'),
   '<b>Bold Text</b>',
   document.createElement('i'),
   '<p>Paragraph</p>',
    ...$('input[type="checkbox"]*')
];

$('.all > div*').before(elements);

Last updated