Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to wrap using javascript

How to wrap global-text and button by creating a div.

<div class="content">
      <div class="fl-module global-button"> </div>
      <div class="fl-module global-text"> </div>
      <div class="fl-module button"> </div>
      <div class="fl-module text"> </div>
</div>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Create an element and append it to the place you want it. Select the elements you want to be in that element and append them to that new div.

//Select the elements you want inside
const divs = document.querySelectorAll(".global-button, .global-text");

// create the div to wrap your elements
const wrapper = document.createElement("div");

// add it to the DOM
divs[0].before(wrapper);

// insert the elements into the newly created div
divs.forEach(div => wrapper.append(div));
div {
  padding: 2em;
  margin: .4em;
  border: 1px solid black;
  min-height: 2em;
}
<div class="content">
  <div class="fl-module global-button">a</div>
  <div class="fl-module global-text">b</div>
  <div class="fl-module button">c</div>
  <div class="fl-module text">d</div>
</div>

If you have multiple of these, you need to do it slightly different

function wrapIt(parentElem, selectors) {

  //Select the elements you want inside
  const divs = parentElem.querySelectorAll(selectors);

  // create the div to wrap your elements
  const wrapper = document.createElement("div");

  // add it to the DOM
  divs[0].before(wrapper);

  // insert the elements into the newly created div
  divs.forEach(div => wrapper.append(div));
}

var contents = document.querySelectorAll(".content");
contents.forEach((content) => wrapIt(content, ".global-button, .global-text"));
div {
  padding: 2em;
  margin: .4em;
  border: 1px solid black;
  min-height: 2em;
}
<div class="content">
  <div class="fl-module global-button">a</div>
  <div class="fl-module global-text">b</div>
  <div class="fl-module button">c</div>
  <div class="fl-module text">d</div>
</div>
<div class="content">
  <div class="fl-module global-button">a</div>
  <div class="fl-module global-text">b</div>
  <div class="fl-module button">c</div>
  <div class="fl-module text">d</div>
</div>
<div class="content">
  <div class="fl-module global-button">a</div>
  <div class="fl-module global-text">b</div>
  <div class="fl-module button">c</div>
  <div class="fl-module text">d</div>
</div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading