JavaScript or jQuery can be used to rearrange the position of child divs within parent divs with a single action

I am facing a dilemma with 5 identical divs, each containing text and an image. My goal is to rearrange the child divs so that the text comes after the image in each article. Put simply, I want the image to be displayed above the text in all instances.

All my attempts have resulted in either moving all text divs after the first image div or placing groups of 5 text divs following each image.

The challenge for me lies in ensuring the script only runs once for every parent element.

P.S. The actual image divs contain real images, but for demonstration purposes, emojis are being used here.

<div class="row article1">
    <div class="text-part">text1</div>
    <div class="image-part">🙏</div>
</div>

<div class="row article">
    <div class="text-part">text2</div>
    <div class="image-part">🙏</div>
</div>

<div class="row article">
    <div class="text-part">text3</div>
    <div class="image-part">🙏</div>
</div>

<div class="row article">
    <div class="text-part">text4</div>
    <div class="image-part">🙏</div>
</div>

<div class="row article">
    <div class="text-part">text5</div>
    <div class="image-part">🙏</div>
</div>

Answer №1

Although it may be a bit confusing, you can begin with this.

document.querySelectorAll('.article').forEach(element=>{
  let text = element.querySelector('.text-part');
  element.removeChild(text);
  element.appendChild(text);
})
<div class="row article">
    <div class="text-part">text1</div>
    <div class="image-part">🙏</div>
</div>

<div class="row article">
    <div class="text-part">text2</div>
    <div class="image-part">🙏</div>
</div>

<div class="row article">
    <div class="text-part">text3</div>
    <div class="image-part">🙏</div>
</div>

<div class="row article">
    <div class="text-part">text4</div>
    <div class="image-part">🙏</div>
</div>

<div class="row article">
    <div class="text-part">text5</div>
    <div class="image-part">🙏</div>
</div>

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

Tips for maintaining consistent image dimensions while resizing your browser window

I'm having trouble preventing an image from resizing when I adjust the browser size. If you click on this link and try resizing your browser, you'll see what I mean - the image stays the same. I've attempted various methods but haven't ...

AngularJS postback method fails to trigger

Seeking assistance with my angularJS AJAX postback method. Below is the HTML code I have created: <html xmlns="http://www.w3.org/1999/xhtml" ng-app> <head runat="server"> <title></title> <script src="Scripts/angular.js ...

The show/hide jquery function is functioning perfectly on desktop devices, but issues arise on mobile devices where the divs overlap each other when using the show() method

I need a way to toggle input text boxes based on selection using a select box This is the HTML code snippet: <div class="row"> <div class="form-group"> <div class="col-sm-1 label2"> <label class="control-label ...

Having trouble with processing the binding? Use ko.mapping.fromJS to push JSON data into an ObservableArray

Hey everyone, I'm struggling with my code and could really use some help. I'm new to knockout and encountering an issue. Initially, I receive JSON data from the database and it works fine. However, when I click 'Add some', I'm tryi ...

Preventing Event Propagation in JQuery Across Different Web Browsers

My GUI controls have a complex nested structure, and I need to prevent events from propagating up the DOM tree when they are clicked or changed. This functionality must be consistent across all browsers. Currently, I am using some cumbersome JavaScript co ...

How to initiate a refresh in a React.js component?

I created a basic todo app using React, TypeScript, and Node. Below is the main component: import * as React from "react" import {forwardRef, useCallback, useEffect} from "react" import {ITodo} from "../types/type.todo" import ...

Having trouble submitting a form in React JS

I'm facing an issue with my form where I am trying to print the data in console upon submission, but for some reason it's not working. The form is not submitting and I can't figure out why. Below is the code I have written. Any help would be ...

Trigger ajax request automatically without the need for a button click

Is there a way to make an AJAX call in jQuery without the need for clicking a button, but rather have it execute when the page loads? This is the button: <div> <form method="post" action=""> <button value="cars" type="submit" i ...

What could be causing the 'Invalid element type' error to occur in my React Native application?

`import { StyleSheet, Text } from 'react-native'; import { Provider } from 'react-redux'; import { store } from './store'; import { HomeScreen } from './screens/HomeScreen'; import { SafeAreaProvider } from 'rea ...

Receiving messages in AngularJS with the help of Strophe.js

My XMPP client in AngularJS, inspired by this stackoverflow link. <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5d282e382f1d282e382f7331323e3c31">[email protected]</a> can send messages successfully, but <a hr ...

Cleaning up HTML5 video content

I have been on the search for a solution that would allow me to "scrub" through HTML5 video. So far, I have not come across one and was considering developing my own. However, before diving into that process, I wanted to seek advice from the community here ...

Guide to transferring array information from one Vuejs route to another

I'm facing an issue with passing an array from one Vuejs route to another. Specifically, I need to pass the array from home.vue to post.vue. In my route.js file for post.vue, I have: { path: '/post/:cart', name: 'post', com ...

When HTML elements are dynamically inserted through JavaScript using quilljs, they may cause conflicts with the layout properties

I am currently working on creating a simple webpage layout similar to that of Stack Overflow, with a sidebar and a main content area that can scroll. In my case, the content area is intended to host a QuillJS text editor. To integrate the QuillJS editor i ...

Is there a better option than the Korean Syllable PHP or JavaScript Romanizer?

I am currently working on creating a transliterator from the Korean alphabet (hangul) to the Latin alphabet (romanization). However, after several unsuccessful attempts using a simple associative array, I have realized that it may not be the most effective ...

What is the best way to extract information from my MYSQL database and represent it as an array?

Currently, I am working on a backend API that utilizes a MySQL database. My goal is to extract data from this database and utilize it to plot latitude and longitude points on a map using the Google Maps API. To achieve this, I have integrated the Gmaps API ...

Mapping variables to attributes in polymers

I'm working with a polymer element that looks like this: <polymer-element name="foo" attributes="bar"> <template> <core-ajax url="url.php" params="{{ {last: bar} }}"></core-ajax> {{bar}} </template> </polymer-element& ...

Issue with JavaScript HTML Section Changer not functioning properly

I need to implement a button that switches between two pages when pressed. Although the code seems simple, I am struggling to make it work. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"& ...

Utilize the display flex property within a nested flex container

I can't seem to get my link text centered both vertically and horizontally, it keeps aligning to the left. I was under the impression that using flex-based alignments would solve this issue. I have gone through the information on using a flex item as ...

Displaying an 'undefined' message in a JavaScript alert box

Hello! Just made the switch from C#/C++ to JavaScript recently and I'm really enjoying it. I've encountered a behavior that has me scratching my head, can anyone help explain? So here's what's happening: when I run this script, I see ...

The controller in Rails3 is not receiving the parameters properly when using coffeescript and ajax

Currently, I am working on setting up a sorting list using Rails3 and coffeescript. Earlier, I encountered a routing problem which I was able to resolve with the kind assistance from someone. However, I now face a different issue where my data parameter is ...