Within Each Parent div, Take an element located in a child div and append it to a different child div

Reading Time: 3 minutes

I’ve got a few divs on a page which consist of Car details; I need to take “add to cart ” button located within div.list-item-content__button-wrapper and append it to the Title container div (div.list-item-content__title).Simply I need all “add to cart ” buttons under Title of every parent div.
 

/*<div class=”list-item-content”>
<div class=”list-item-content__text-wrapper”> 
TITLE <div class=”list-item-content__title”>Car interior</div>
</div><hr>
What is Lorem Ipsum?
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.


<div class=”list-item-content__button-wrapper”>
<button name=”favorite”>

  Add To cart 
</button>
</div>
</div> 
 
<div class=”list-item-content”> 
<div class=”list-item-content__text-wrapper”>
TITLE<div class=”list-item-content__title”>Car interior</div>
</div><hr>
What is Lorem Ipsum?
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

<div class=”list-item-content__button-wrapper”> 
<button name=”favorite”> 

  Add To cart
</button> 
</div>
</div>*/

TITLE

Car interior


What is Lorem Ipsum? Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

Add To cart

TITLE

Car interior


What is Lorem Ipsum? Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

Add To cart



1. .find() Method: This method is used to get all the filtered descendants of each element in the current set of matched elements.

2. .closest() Method: This method is used to get the first ancestor of the selected element. The ancestors can be parents, grand-parent, great-grandparents, etc.

3. The .append() method: inserts the specified content as the last child of each element in the jQuery collection (To insert it as the first child, use .prepend()

So Following is result after using .closest(), .find() , .append in jquery

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>
<script>
         

$(function() { 
  $('div.list-item-content div.list-item-content__title').each(function() {
   $(this).append($(this).closest('div.list-item-content').find('div.list-item-content__button-wrapper'));
  });
});
</script>

TITLE

Car interior

Add To cart


What is Lorem Ipsum?
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

TITLE

Car interior

Add To cart


What is Lorem Ipsum?
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

7 thoughts on “Within Each Parent div, Take an element located in a child div and append it to a different child div”

  1. My coder is trying to convince me to move to .net from PHP.

    I have always disliked the idea because of the costs. But
    he’s tryiong none the less. I’ve been using Movable-type on a variety of websites for about a year and am worried about switching to another platform.
    I have heard great things about blogengine.net. Is there a way
    I can import all my wordpress content into it?
    Any help would be really appreciated!

Leave a comment