Ways to automatically move a DIV downward depending on the element before it

I have a set of DIV elements, all sharing the same class (.ms-acal-item). My goal is to determine the position of each one, compare it with the one above, and adjust it downward if necessary. This is to address an issue on a SharePoint 2013 calendar where event tiles are overlapping. Here's the code I've written so far:

$(function() {

   $('.ms-acal-item').each(function(i, obj) {
    var tile = jQuery(this);
    var prev = tile.prev();
    var prevPos = prev.position();
    var tilePOs = tile.position();
    var curTop = parseInt(tilePos.top);
    var newTop = parseInt(prevPos.top) + 30;
    if(curtop !== newTop){
    tile.css('top', newTop);
    }
   });

});

Here is an example of how the content generated by SharePoint looks: https://i.sstatic.net/ZGS0S.jpg

Each tile is 20px high and positioned absolutely in-line. The aim is to compare the position of each tile with its predecessor and move it down by 30px if needed (20px for the tile and 10px between them). However, my current code doesn't seem to be having any effect on the tile positions.

What could I be doing incorrectly?

Answer №1

When working with the first element, a logic issue arises due to the lack of a previous element. To address this issue and correct some variable case errors in your code, here is an updated version below. (After reviewing, I noticed that the style was changed from top to margin-left; please adjust it accordingly)

$(function() {
   $('.ms-acal-item').each(function(i, obj) {
    var tile = $(this);
    var prev = tile.prev();
    if (typeof prev.position() !== "undefined") {
        var prevPos = JSON.parse(JSON.stringify(prev.position()));
        var tilePos = JSON.parse(JSON.stringify(tile.position()));
        var curTop = parseInt(tilePos.top);
        var newTop = parseInt(prevPos.top) + 30;
        if(curTop !== newTop){
        tile.css('margin-left', newTop);
        }
    }
   });

});

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

Using Div in Bootstrap to Overlay Text on an Image

Trying to overlay text on an image using the code snippet below; https://i.sstatic.net/7jkEC.png <div class="wrapper"> <img src="https://cdn0.vox- cdn.com/thumbor/qLH7593e3D2SSSIdkwgBWdYhjjk=/289x0:802x684/400x533/filters:forma t(webp)/cdn0.vo ...

Why can't I seem to print the total sum of numbers stored in the React-Redux state?

Seeking assistance to understand why the "sum" value, which represents the sum of two numbers entered in the input boxes labeled "numOne" and "numTwo", is not being displayed on the user interface. Please refer to AddNum.jsx for more information. Below is ...

The mistake occurs when attempting to access a class property generated by a class constructor, resulting in a type error due to reading properties of

I'm having trouble building an Express API in TypeScript using Node.js. I am new to Express and I have been learning Node, JavaScript, and TypeScript since 2022, so I apologize if the question is not too complex. The issue I'm facing is trying to ...

Clicking on a table row by utilizing a variable

Here's the code I've been working with: $("table tr:eq(0) td:first-child").click(); Initially, everything was running smoothly. However, now I'm attempting to replace the hard-coded '0' with a variable value. Here's how I at ...

How to overlay text onto an image using CSS

Is there a more efficient way to overlay text on an image without using position: absolute;? Dealing with position: absolute; for different screen sizes isn't the ideal solution for me. I've searched for alternatives, but all I keep finding is th ...

"Implementing a jQuery dialog box that sends JSON response to a different page rather than the current

Currently, I am working on an MVC application where I need to insert properties of certain objects. To achieve this, I have created a modal popup using jQuery dialog. In order to avoid any interference with other user actions, I have implemented an Ajax.Be ...

VueJS allows you to trigger a Bootstrap modal using pure Javascript, eliminating the need for jQuery

I am currently developing an application with Vue.js. In the login process of my application, I want to display any potential errors using a Bootstrap modal instead of the traditional alert. Is there a method to trigger the modal using JavaScript without r ...

Provide the Ajax caller with a JSON object exclusively

Is there a way to retrieve only the array in my success callback instead of the entire HTML page? $.ajax({ 'URL': 'getData.php', success:function(data){ alert(data); //I'm currently getting the whole html file } }) g ...

`The functionality of Material UI Grid item seems to be malfunctioning`

I currently have the following code snippet: <div style={{ flexGrow: 1 }}> <Grid container spacing={0} style={{ width: '100%' }}> <Grid item xs={12} sm={6}> Left Side </Grid> ...

Position a div to float in the top right corner without overlapping its sibling header

Is there a way to position a div in the top right corner of a section without overlapping the text of a h1? This is the HTML code: <section> <h1>some long long long long header, a whole line, 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6</h1> < ...

Connecting two COTURN servers for seamless communication

Currently, I have a total of 5 webRTC peers connected through the COTURN server (turnServer1). These peers are all behind symmetric NAT, requiring the use of the TURN server to establish connections. However, due to the media streams with audio and video b ...

What is the correct way to incorporate Regular Expressions in Selenium IDE coding?

Using regular expressions to check for a correct answer from a prompt has been challenging. The current expression seems to be giving unexpected results, marking valid answers as false. For instance, when inputting the number 3, Selenium indicates that the ...

"Error: Command 'npm' is not recognized as a valid internal or external command" encountered while using create-react-app

Why won't npm work for me? I've been trying to dive into the world of React and kickstart my learning journey. With Node installed, along with the create-react-app package, I thought I was all set. When I run commands like npm -v or create-reac ...

Issue with Angular not correctly implementing Bootstrap Tooltip

I've been trying to use Bootstrap Tooltip in my Angular code, but I'm having trouble getting it to work. I thought the Angular expressions like: {{o.client_name}}, {{o.client_name}} would work, but they are not. Can someone help me understand why ...

Using ReactJS to return a component from a function

Working with React, useState, and React-Icons. I am trying to implement a dropdown menu without using Bootstrap. My goal is to change the icon upon clicking to trigger a function, but currently, it only displays the raw SVG details. Any suggestions on how ...

Building a multi-form application with HTML5 validation and Vue.js

I am working on a vue.js form that consists of multiple steps and requires HTML5 validation to be simplified. Here is a snippet of the code: <template> <div> <div v-if="activeForm === 1"> <h2> ...

Zend and qTip compatibility issues are causing problems

I am attempting to implement a help button in a Zend form using qTip 2. The button consists of a simple image, and my goal is to display a pop-in when the image is clicked. Here is my code: In Booostrap.php: protected function _initView() { ... ...

What is the best way to position text at the bottom of an image using CSS?

Utilizing the jQuery Caption Plugin to showcase the image alt text when hovering over an image. Currently, the text is displaying at the top but I would like it to show at the bottom. Any suggestions on how to remedy this? Desired Outcome: Check out the ...

Modify the color of a unique word within a cell in a gridview

How can I change the color of specific keywords in a gridview cell without affecting all words? Here is the sample code: protected void gvContents_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) ...

arranging rows and columns in bootstrap version 4.1

Currently, I have a structure with rows and columns that I need to rearrange for mobile responsiveness. The main row consists of three columns, each containing rows in a vertical layout. Here is an example: https://i.sstatic.net/tFEhs.png For mobile view ...