---
title: "Why are use the before and prepend method in JQuery? what is difference between the before and prepend method?"  
description: "Why are use the before and prepend method in JQuery? what is difference between the before and prepend method?"  
author: "Ethan Karla"  
published: 2021-07-28  
canonical: https://answers.mindstick.com/qa/93805/why-are-use-the-before-and-prepend-method-in-jquery-what-is-difference-between-the-before-and-prepend-method  
category: "web application"  
tags: ["web application development", "web development"]  
reading_time: 1 minute  

---

# Why are use the before and prepend method in JQuery? what is difference between the before and prepend method?

Why are use the before and prepend [method in JQuery](https://www.mindstick.com/forum/156670/has-method-in-jquery)? what is [difference](https://www.mindstick.com/blog/398/difference-between-object-type-and-var-type) between the before and prepend method?

## Answers

### Answer by Ravi Vishwakarma

The jQuery before [method](https://www.mindstick.com/articles/23411/the-most-effective-method-to-find-the-perfect-small-business-phone-system-for-your-business) is used to insert content into the HTML page before the selected elements. Appends the given content to a specific location in before ().\
Same happens in prepend () it also appends the given data but like this child node.The prepend () method is used to insert the content at the beginning of the selected elements (as the first child).

> The main difference between before() and prepare() is that before() adds the content first, but prepend() adds the content first treats like as a child.

```
Syntax
Before () syntax
 $(selector).before(content)
 $(selector).before(content,function(index))
Prepend () syntax
 $(selector).prepend(content,function(index,html))
 $(selector).prepend(content,function(index,html))
```

```
<!DOCTYPE html>
<html>
<head>
<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js'></script>
<script>
$(document).ready(function(){
  $('button').click(function(){
    $('p').before('<p><b>Hello world! by before() </b></p>');
    $('p').prepend('<p><b>Hello world! by prepend() </b></p>');
  });
});
</script>
</head>
<body>
<button>Insert content by before() and prepend()</button>
<p>This is a paragraph.</p>
</body>
</html>
```

Output: ![Why are use the before and prepend method in JQuery? what is difference between the before and prepend method?](https://answers.mindstick.com/questionanswer/30937acc-5013-4c42-a109-6dd6c71593d4/images/f31bb19d-e6a7-4e83-af88-6f6e9d5cfcac.png) \
\


---

Original Source: https://answers.mindstick.com/qa/93805/why-are-use-the-before-and-prepend-method-in-jquery-what-is-difference-between-the-before-and-prepend-method

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
