xxxxxxxxxx
// Assuming you have included jQuery library in your HTML
// Find the existing div element
var existingDiv = $('div#existingDiv');
// Create a new div element to be added
var newDiv = $('<div>', {
id: 'newDiv',
text: 'This is the new div'
});
// Insert the new div before the existing div
existingDiv.before(newDiv);
xxxxxxxxxx
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>before demo</title>
<style>
p {
background: yellow;
}
</style>
<script src="https://code.jquery.com/jquery-3.5.0.js"></script>
</head>
<body>
<p> is what I said</p>
<script>
$( "p" ).before( "<b>Hello</b>" );
</script>
</body>
</html>
xxxxxxxxxx
// Create a new element
var newElement = $("<p>New Element</p>");
// Select the target element where you want to prepend the new element
var targetElement = $("#targetElement");
// Use the before() method to prepend the new element before the target element
newElement.before(targetElement);