Duplicate the initial div and insert it after the following div by targeting its class name

I have a structure that looks like this:

<div class="container">
    <h1 class="header-div">Title 1</h1>
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
    <div class="image-div">image1</div>
</div>
<div class="container">
    <h1 class="header-div">Title 2</h1>
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
    <div class="image-div">image2</div>
</div>
<div class="container">
    <h1 class="header-div">Title 3</h1>
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
    <div class="image-div">image3</div>
</div>


I plan to extract h1 and add it directly after .image-div for each block.

Answer №1

There are a couple of things to keep in mind:

  • The appendTo function will add the content inside the specified div, not after it.
  • If you don't iterate through each element, all elements will be cloned together and appended for every image-div.

Here's what you can do:

$(".header-div").each( function(){
  $(this).clone().insertAfter( $(this).parent().find( ".image-div" ) );
});

Or alternatively:

$(".header-div").each( function(){
  $(this).clone().appendTo( $(this).parent() );
});

Answer №2

Check out this Fiddle

Here is a way to achieve the desired result:

const mainContainers = $(".main-container");
$.each(mainContainers, function(index, element){
    $(element).find('.title').clone().appendTo($(element).find(".image"))
} )

The each method iterates through an array of main container elements that contain the image and title divs, moving the title to a different position in the DOM.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to instantiate objects, arrays, and object-arrays in an Angular service class?

How can I nest an object within another object and then include it in an array of objects inside an Angular service class? I need to enable two-way binding in my form, so I must pass a variable from the service class to the HTML template. trainer.service. ...

Selecting elements by their name using vanilla JavaScript

I am facing an issue where I have to assign a value to a checkbox based on certain variables. The challenge lies in the naming convention used in the HTML I am working with: <input id="jc_1" type="checkbox" name="jc[1]"> <input id="jc_2" type="c ...

I am confused by this javascript syntax: const { headers, method, url } = request;

There is confusion regarding the interpretation of this code snippet const { headers, method, url } = request; located in this tutorial https://nodejs.org/en/docs/guides/anatomy-of-an-http-transaction/ ...

Attempting to enlarge an image by hovering over it proves to be futile

Currently, I am exploring CSS and attempting to enlarge 2 images when they are hovered over. Unfortunately, the desired effect is not working. .imagezoom img { position: relative; float: left; margin: 50px 5px 0px 42px; border-color: rgba(143, 179, 218, ...

Convenient ways to connect data between a database and Firebase authentication system

After beginning a basic app using Firebase 3 and Angular 1, I implemented an authentication system through Firebase for user registration and login. Currently, I am storing message data in the Firebase database. My main question is: how can I connect user ...

Deactivate HTML column using the power of Jquery

Is there a way to disable a specific column in a table with multiple columns by clicking on the corresponding checkbox in the header? I am looking for a solution using jQuery to get the currently clicked column from the checkbox. ...

What is the best method for deleting an uploaded image from a box?

My code is quite lengthy, so I'll just showcase the JavaScript portion here. Here is a snippet of my JavaScript code: <script type="text/javascript"> let current = 0; for(let i = 0; i < 5; i++) { $('#thumbnail-view- ...

"Modifying state within a child component and utilizing the refreshed value in the parent component

I'm currently working on creating a simple header mini cart with a cart item counter in NextJS. I'm utilizing the form state value in the header component and then passing that value to the child components of the header where the numerical quant ...

The implementation of binding complex types through Ajax in the model

I'm currently working on implementing a Controller with a method structure like this: public ActionResult Insert(Author author) { //perform some operations... } The Author type is defined as follows: public class Author { public string FirstNam ...

Using SVG and CSS to color multiple paths with individual colors

I am working with an svg that contains various paths, ellipses, and shapes each with different fill colors such as red and blue. When I combine them into a sprite, I want to be able to change the fill color on hover using CSS. Typically, I would remove the ...

Dynamically generating subitems for a menu

I have created a Menu using CSS/Bootstrap in my asp.net application, but I need the submenus to be generated dynamically based on the user's role at runtime. Does anyone have any suggestions on how I can achieve this? I am looking for ideas on how to ...

Removing an unused JS file from a page using Jquery/Javascript

On my single HTML page, I have 4 different sections with corresponding views that are shown or hidden based on button clicks. Each view has its own JS file, and I load the necessary one based on the current page like this: //Page1 if (current_pag ...

jQuery Mobile dual range slider functioning but experiencing some glitches

I successfully created a dual range slider by stacking two sliders on top of each other using the jQuery Mobile framework. In addition, I have implemented JavaScript to ensure that the left thumb does not exceed the right one. However, I have encountered ...

Is it possible to locate the Bower configuration file in Debian if running as root?

Hey there, I've been trying to execute a "bower install" command on a project I'm working on, but unfortunately, I keep running into an error. Error details: Since bower is a user command, it shouldn't be run with superuser permissions. If ...

The chai property "matchSnapshot" is not valid

https://i.sstatic.net/4wgqq.png After following the instructions provided at this link, I am now in the process of writing basic Jest tests for my React application that uses the Create-React-App starter kit. I came across a recommendation mentioned here ...

Using the same Angular component with varying input values

Recently, I encountered a puzzling issue that has left me unsure of where to even start investigating the possible causes. I am currently utilizing a library called angular-stl-model-viewer, which is based on Three.js. The problem arises when I call a comp ...

How to adjust the size of an SVG image on an HTML webpage

I've got an SVG file nestled inside my HTML code, and one of the cool things I've learned about this format is its ability to stay crisp and clear even when zoomed in. Typically, with a JPEG file, you might store it as a small 50 by 50 icon, the ...

AJAX request missing Authorization header

Having trouble sending an Authorization header with an AJAX request. Despite configuring everything correctly, it seems like there may be a CORS issue. Here is the client-side code snippet: $.ajax({ url: <location>, method: "GET" ...

Creating a glowing shimmer using vanilla JavaScript

After successfully creating the Shimmer Loading Effect in my code, I encountered a hurdle when trying to implement it. The effect is visible during the initial render, but I struggle with utilizing it effectively. The text content from my HTML file does no ...

Preview of Hoverbox Caption

I'm working on a website using the Hoverbox image gallery (http://host.sonspring.com/hoverbox/) and I'm trying to include captions for each image preview that appears when hovering over the enlarged image. However, I am facing difficulty as I hav ...