← Back to Index

.before()

Inserts content, specified by the parameter, before each element in the set of matched elements.

Examples

1. Insert HTML string

$('.content').before('<h2>Introduction</h2>');

Result

<h2>Introduction</h2>
<div class="content">...</div>

2. Insert Element

const $icon = $.t('<i>', { class: 'icon-star' });
$('button').before($icon);

Result

<i class="icon-star"></i>
<button>...</button>

Parameters

Parameter Type Description
content string | element | SR HTML string, DOM element, or SR object to insert before each matched element.

Returns: The original SR object for chaining.