Switching Column Content with jQuery on Mobile Devices

In my design, I have a dynamic two-column layout created using Twitter Bootstrap 3.

The layout switches between image-text and text-image alignment for each row. The goal is to adjust it for smaller screens (less than 768px) so that rows with images on the right side display the image above the text in a single-column view.

Rows with the image on the right are identified by the even class.

Below is the HTML structure of an even row:

<div class="row">
    <div class="col-sm-6 left evenleft">
        text
    </div> <!-- end col-sm-6 -->
    <div class="col-sm-6 right evenright">
        image
    </div> <!-- end col-sm-6 -->
</div> <!-- end row -->

The jQuery code snippet below attempts to achieve this behavior:

function swap() {
    $('.even').each(function () {
        var left, right;
        left = $(this).find('.evenleft').html();
        right = $(this).find('.evenright').html();
        if ($(window).width() < 768) {
            $(this).find('.evenleft').html(right);
            $(this).find('.evenright').html(left);
        } else {
            $(this).find('.evenleft').html(left);
            $(this).find('.evenright').html(right);
        }
    });
}
$(function () {
    swap();
});
$(window).resize(function () {
    swap();
});

While the functionality works as expected initially, resizing the browser causes issues with the layout adjustment.

I am currently facing challenges in resolving this problem.

Answer №1

To achieve the desired layout, you can utilize the classes .col-sm-pull-6 and .col-sm-push-6:


<div class="row">
    <div class="col-sm-6 col-sm-push-6">
        image
    </div> <!-- end col-sm-6 -->
    <div class="col-sm-6 col-sm-pull-6">
        text
    </div> <!-- end col-sm-6 -->
</div> <!-- end row -->

http://plnkr.co/edit/0f1Id9VYZlRel66OeKni?p=preview

Answer №2

To achieve the desired layout, consider positioning the image to the right and floating it. It is likely that JavaScript will not be necessary for this task.

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

Creating an Angular 7 Template using HTML CSS and Javascript

I recently acquired a template that comes with HTML, CSS, and Javascript files for a static webpage. My goal is to integrate these existing files into my Angular project so I can link it with a Nodejs application to create a full-stack webpage with backend ...

Easy steps for importing node modules in TypeScript

I'm currently navigating the world of TypeScript and attempting to incorporate a module that is imported from a node module. I have chosen not to utilize webpack or any other build tools in order to maintain simplicity and clarity. Here is the struct ...

React higher order component (HOC) DOM attributes are causing the error message 'Unknown event handler property' to be triggered

I recently created a Higher Order Component (HOC) using recompose, but I'm encountering a React Warning every time the props are passed down. Warning: Unknown event handler property `onSaveChanges`. It will be ignored. All my properties with a speci ...

Having trouble retrieving the JSON value from a jQuery request response?

Is there a way to retrieve the variable returned from an AJAX request using jQuery? .done(function (response, textStatus, jqXHR){ console.log(response); }) The response displays as {"msg":"ERROR: Record Not Saved"} However, attempting to access resp ...

How can I retrieve the SID received in a different tab using MSAL.js?

I have successfully integrated MSAL into a client-side library, and things are going smoothly so far. My next goal is to enable Single Sign-On (SSO) by following the instructions provided in the documentation at https://learn.microsoft.com/en-us/azure/act ...

Implementing the expand and collapse functionality to the Discovery sidebar on the DSpace 4.2 xmlui platform

I recently began using DSpace and I am attempting to implement an expand/collapse feature in the Discovery sidebar of DSpace 4.2 xmlui using the Mirage theme. After finding some helpful jquery code, I attempted to add this functionality by placing the js f ...

Connect the scroll wheel and then proceed to scroll in the usual way

There are two div elements with a height and width of 100%. One has the CSS property top:0px, while the other has top 100%. I have implemented an animation that triggers when the mousewheel is used to scroll from one div to the other. The animation functi ...

A guide on accessing the href attribute of an HTML tag using jQuery

My goal is to extract the ID from the href attribute of an a tag on my webpage. First, I need to prevent the page from refreshing before performing this task. The example a tags look like this: '<a href="/ArtPlaces/Delete/5">Delete</a>&ap ...

Tips for utilizing string interpolation in the style tag of an Angular component

@Component({ selector: 'app-style', template: ` <style> .test { color: {{ textColor }} } </style> ` }) export class StyleComponent { textColor = "red"; } The current method doesn't appear to b ...

In order to locate a matching element within an array in a JSON file and update it, you can use Node

Good day, I have a script that updates the value in a JSON file const fsp = require('fs').promises; async function modifyNumberInFile() { try { let data = await fsp.readFile('example.json'); let obj = JSON.parse(dat ...

The Visualforce page is not displaying page breaks correctly for cells with rowspan in the table

Having encountered a similar issue to this question: table rowspan page break On my Visualforce page, I have an HTML table with a custom rowspan in one column (varies based on the number of spanned rows) and I'm experiencing a styling problem (see is ...

Is there a way to prevent elements from resizing when zooming in or out?

What is the best way to prevent elements from resizing on a standard website when users zoom in (CTRL +)? ...

Error: Certain Prisma model mappings are not being generated

In my schema.prisma file, I have noticed that some models are not generating their @@map for use in the client. model ContentFilter { id Int @id @default(autoincrement()) blurriness Float? @default(0.3) adult ...

A step-by-step guide on deleting an element within a div using jQuery

I am attempting to delete an HTML element using JQuery like this $('#' + divId + ' .settings_class').remove('.print_settings'); This code does not result in an error or remove the specified html element, however, the selecto ...

Retrieve the object when hovering over an element in the grid

In a collection of 16 items belonging to the same class, I am looking for a way to implement a mouseover effect that changes the opacity of only the specific item being hovered over and not all items in the class. Check out this link for an example ...

Rotating the transform causes the div to be pushed beyond the screen boundaries and renders

html, body { margin: 0; padding: 0; overflow-x: hidden; } /*HEADER*/ #header { background: blue; background-attachment: fixed; background-size: cover; width: 100vw; height: 100vh; } .btn-1, .btn-2, .btn-3 { background ...

Puzzled on how to include a string in the parameter for a GET request?

Following the initial instruction to make a get request for a turtles route and send a JS object of turtles with their colors, I successfully implemented it like this: app.get('/turtles', function(req, res) { data = { Raphael: "Red", Le ...

Reactjs Promise left hanging in limbo

How can I resolve the pending status of my promise? I have a modal with a form submit in it, where I am trying to retrieve the base64 string of a CSV file. While my code seems to be returning the desired result, it remains stuck in a pending state. c ...

I can't figure out why my code isn't functioning properly. My goal is to have a PDF file generated when the submit button is clicked on a form, and for a

Hello everyone, I am new to the world of programming, but I have been assigned a project that involves using jQuery, JavaScript, html2pdf, and Bootstrap 5. I seem to be facing some issues with my code. Can someone please help me identify what's wron ...

Incorporate JSON data into a subsequent AJAX call when coding in JavaScript

I am facing an issue where I need to use data returned from an ajax request to construct a string and then post it back using ajax. However, the variable I'm assigning the data to is not within the scope of the second request, resulting in a 'var ...