This post will discuss how to move an element inside another element in JavaScript and jQuery.

jQuery Solution:

There are several ways to move an element inside another element with jQuery:

1. Using append() method

The .append() method inserts the specified content after the last child of the target.

JS


HTML


CSS



Edit in JSFiddle

2. Using prepend() method

The .prepend() method inserts the specified content before the first child of the target.

JS


HTML


CSS



Edit in JSFiddle

3. Using appendTo() method

The appendTo() method is similar to the append() method but has a different syntax with respect to the placement of the content and target.

JS


HTML


CSS



Edit in JSFiddle

4. Using prependTo() method

The prependTo() method is similar to the prepend() method but has a different syntax with respect to the placement of the content and target.

JS


HTML


CSS



Edit in JSFiddle

 
Note that jQuery also offers the insertAfter() and insertBefore() methods, which inserts the content after or before the target, respectively.

JavaScript Solution:

In plain JavaScript, you can use the appendChild() method to move the existing source element in the document to the target element.

JS


HTML


CSS



Edit in JSFiddle

That’s all about moving an element inside another element in JavaScript and jQuery.