Utilize the splice function when resizing the window based on specific breakpoints

On a series of div elements, I have implemented some JS/jQuery code that organizes them by wrapping every three elements in a container with the class .each-row.

<div class="element"></div>
<div class="element"></div>
<div class="element"></div>
<div class="element"></div>
<div class="element"></div>
<div class="element"></div>

results in:

<div class="each-row">
    <div class="element"></div>
    <div class="element"></div>
    <div class="element"></div>
</div>
<div class="each-row">
    <div class="element"></div>
    <div class="element"></div>
    <div class="element"></div>
</div>

The JavaScript code used for this functionality is as follows:

// Grab all the .bricks
var brick = $('.container .brick');
// Iterate through all the .bricks
for (var i = 0; i < brick.length; i += 3) {
    // Create container
    var brickContainer = $('<div />', {
        class: 'each-row'
    });
    // Wrap all the .bricks inside the container
    brick.slice(i, i + 3).wrapAll(brickContainer);
}

This solution works effectively, however, I aim to incorporate breakpoints so that under different screen sizes, the elements are organized differently. When the screen size is 767px or below, each .brick should be wrapped in an each-row, and if it's less than 1224px, wrap every two bricks, otherwise wrap every three.

I have already achieved this through the use of the resize function and even provided a demonstration on JSFiddle. The only issue encountered is that due to resizing, the each-row gets added at every pixel change. When inspecting the DOM during the resize process on JSfiddle, you'll notice the continuous addition of each-row.

Any suggestions on how to resolve this would be greatly appreciated.

Thank you, R

Answer №1

Make sure to first unwrap the elements before wrapping them again.

function adjustGrid() {

    $('.each-row').each(function(index, eachRow){
        $(eachRow) // choose the row
            .children()  // select its children
            .detach()  // disconnect them from the row
            .appendTo($(eachRow).parent()); // add them to the row's parent
        $(eachRow).remove(); // delete the empty row
    });
    // remainder of your code here
});

I have made updates to your example on fiddle: http://jsfiddle.net/b2YKZ/4/

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

Is there a way to convert a button into clickable text that performs the same function as the original button?

Seeking a solution to transform a button into clickable text with the same functionality. Explored resources like Google, StackOverFlow, and Youtube for answers. This is the current code snippet representing the button: <button onclick="mainApp.l ...

What are the solutions for resolving problems with the display and functionality of a multi-page form in Internet Explorer?

I am currently working on a form and you can view it at this link: http://jsfiddle.net/pPWr3/14/ While the form displays correctly in Chrome and Firefox, I am experiencing display and functionality issues when using Internet Explorer 9. The problems inclu ...

Scrolling down a jQuery div after each new item is added.OR

I have a div acting as a chat feature, where messages are appended based on user actions. I am trying to make it so that the div automatically scrolls down after each message is added, but I'm encountering some issues with this functionality. You can ...

Control the button's activation status by accepting or unaccepting the two checkbox conditions

In my search through stackoverflow for help on enabling/disabling a button conditionally, I found some assistance but nothing quite matched what I needed. My situation involves two checkbox conditions rather than just one. The button should only be enable ...

Ways to initiate a transition upon clicking a button, causing it to switch from being hidden (`display: none`) to visible (`display: block`)

I'm attempting to create a smooth transition effect when a button is clicked, similar to a toggle function, where the content below it seamlessly shifts instead of abruptly moving. The classic example of this is the Bootstrap navbar hamburger menu, wh ...

Is it possible to employ the hide()/show() functions for elements that were concealed 'manually'?

In my upcoming project, I am planning to create a web page using JSP. To achieve the desired functionality, I want to hide an element in the JSP and then use jQuery to show it later. // This is how I have attempted to hide the div in the index.jsp file: & ...

What are some ways to customize the appearance of React Semantic components?

Is there a way to apply CSS for react semantic UI when using create react app? I have installed semantic-ui-react and included the CSS CDN: loginForm.js: import React from "react"; import { Button, Form, Header } from "semantic-ui-react"; import styles f ...

Unable to display background image on Webpage due to blank page issue while uploading it with HTML and CSS

I've been attempting to build a webpage with an image as the background, but I seem to be facing some issues. Instead of seeing the desired image, all I get is a blank white page. The folder named "images" is located in the same directory as my HTML a ...

Leverage the power of Laravel and ReactJS to beautifully display images

Currently, I am working on a project where I am incorporating ReactJS into a Laravel blade file. To get started, I included the React CDN and began writing code within the script tag. Everything seems to be going smoothly so far, however, I have encountere ...

Display additional information upon hovering without disrupting the neighboring elements

When I hover over a component, I want to display more details and scale it up. However, this action ends up displacing the surrounding components. Take a look at the screenshot for reference: Below is the code snippet where I defined the styling for an MU ...

The rendering of the component is experiencing issues when using react-router

function App() { return ( //Following BEM naming convention <div className="app"> <div className="app__body"> <Sidebar /> <Chat /> <Router> <Routes> ...

What is the best way to create square editor tabs in Eclipse without using swt-border-radius?

The design of Eclipse's rounded and/or swooshing tabs is starting to feel outdated in today's modern era. I thought that by adding the following line in my default.css file, I could get rid of the rounded corners: swt-corner-radius: 0px Howeve ...

Is it possible for a Django view to send JSON data back upon a successful Ajax request?

I've tested both JsonResponse and HttpResponse (along with json.dumps), but even though ajax returns successfully, the returned JSON cannot be parsed by $.parseJSON(returned_json). I'm certain that the issue does not lie with parsing ($.parseJSO ...

Troubleshooting: Unable to delete data using $http in AngularJS

When using the $http service in Angular JS to call an API for deleting a message, I am receiving a successful response but the value is not actually being deleted. Interestingly, when I directly access the same API in my browser, the message gets deleted s ...

The HTML element failed to be inserted

Currently, I am involved in a project based on .NET Core for my organization. This project entails loading work orders from our SQL database using Entity Framework and then filtering them to display as markers on a map via the Google Maps API for our insta ...

Node.js in action with XmlHttpRequest

I'm having trouble making an XMLHttpRequest call from my client-side JavaScript to my Node server. It seems like nothing is happening and I'm a bit new to this concept. Here's the JavaScript function I've written: function sendTokenToS ...

Efficiently Parsing for PostgreSQL WHERE Clauses Using AJAX and PHP

Currently, I am facing a situation where I need to retrieve activities with corresponding version numbers. The challenge lies in the fact that some activities have only one version, while others have multiple versions. These activities are obtained through ...

What rules should be followed in Python when it comes to using nested parentheses in functions?

Currently in the process of deleting my account from this platform, I am intentionally adding unnecessary content to this post. Please refrain from restoring the previous content. - Original Poster ...

Navigating through various versions of admin-on-rest can be perplexing

This question is likely directed towards maintainers. Currently, I am using the stable version of admin-on-rest (https://www.npmjs.com/package/admin-on-rest) which is at 1.3.4. It seems that the main project repository is only receiving bug fixes, while ...

Save the click value for future use

My appointment form is divided into separate panels. At Step 1, if a user clicks on London (#Store1), I want to hide the Sunday and Monday options from the calendar in panel 5. Essentially, I need to save this click event so that when the user reaches th ...