1

I want to split dynamic div content into two div content with specific height using jQuery or JavaScript?

enter image description here

for example here you can see 10 number will come and want to split into two divs. i am using number here but any content will come so how we can do this? please help. thanks in advance.

<div id="data">Lorem ipsum dolor sit amet, consectetur adipiscing elit.Vestibulum aliquam eget massa eu commodo. Vivamus pharetra ante in orci pharetra, vitae pulvinar dolor viverra. Vestibulum massa metus, pretium ut elementum quis, molestie et quam. Sed vitae nisi lobortis, condimentum orci sed, hendrerit elit. Ut erat mauris, posuere a metus vitae, accumsan tincidunt risus.Nulla nec semper nisi.</div>

Simple content want to split into two div with specific line. suppose div has 10 lines and i want to split content from line 8. so line no 1 to 7 in one div and line no 8 to 10 in other div.

sabir sam
  • 83
  • 1
  • 6
  • How data come? please post your code. – Simone Rossaini Jan 22 '21 at 09:56
  • @SimoneRossaini Right now i am trying with static data. – sabir sam Jan 22 '21 at 09:57
  • 1
    @sabirsam Please show us your code and what you have tried. without it we cant give you a good answer – Carsten Løvbo Andersen Jan 22 '21 at 10:00
  • @CarstenLøvboAndersen i have update my question. Simple content want to split into two div with specific line. suppose div has 10 lines and i want to split content from line 8. so line no 1 to 7 in one div and line no 8 to 10 in other div. – sabir sam Jan 22 '21 at 10:10
  • @SimoneRossaini i have update my question. Simple content want to split into two div with specific line. suppose div has 10 lines and i want to split content from line 8. so line no 1 to 7 in one div and line no 8 to 10 in other div. – sabir sam Jan 22 '21 at 10:11
  • keep in mind that the number of lines might be according to the screen-resolution – john Smith Jan 22 '21 at 10:11

1 Answers1

0

Note: I'm assuming the "dynamic content" is made up of HTML elements contained within the original div.

In order to split the children from one div into another you would do the following:

  1. Create a new div (or clone the old one if you want to recycle properties)
  2. Move the children into the new div
  3. Add extra margin to separate the 2 divs

In JavaScript:

// Assume `div` is the original div

// Create new div
const new_div = document.createElement("div");

// Or clone the old div
// const new_div = div.cloneNode();

const total_elements_kept = 8;

// Move the children of the original div into the new div
Array.from(div.children).slice(total_elements_kept, )
    .forEach(function (elm) {
        new_div.appendChild(elm);
    });

// (optional) Add extra margin to separate the 2 divs
// (Note: this would be better done with CSS)
div.style.marginBottom = "40px";

If the "dynamic content" were to be in text form it would need to be split before following a similar procedure as the above mentioned.

Beni Trainor
  • 346
  • 1
  • 11
  • can you please edit in this codepen https://codepen.io/sabir786/pen/MWjdYpX – sabir sam Jan 22 '21 at 10:28
  • Is still don't quite undestand what you want. You're code only has one element. It would need to be split into `

    ` elements or broken up some other way, like with `
    `.

    – Beni Trainor Jan 22 '21 at 10:39
  • Need to split content into two separate div from one div. – sabir sam Jan 22 '21 at 10:41
  • But, how do you delimit the content? How do you want to separate it? The text as it stands can be split a lot of ways. – Beni Trainor Jan 22 '21 at 10:49
  • I want to spilt content accordingly div fix height. Suupose div has 100px so up till height i want to show content within and another content will show in different div. so i don't want to content cutoff. – sabir sam Jan 23 '21 at 08:53
  • I don't exactly know how you would do this. You might need to calculate the width of the text to determine how much is visible (not overflowing) inside the div. Once you know that you can split the text in two and move the bottom half to the next div. See [this SO answer](https://stackoverflow.com/a/118251/12233224) for more information on this. – Beni Trainor Jan 23 '21 at 10:20
  • Yes, got your point, how we can get exact character or word which want to spilt from into next div – sabir sam Jan 25 '21 at 10:15