Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

146
Views
connecting divs with svg line
  for (el of chart.children) {
    i++
    previous__element = chart.children[i - 1]
    if (el.classList.contains('rule')) {
      //pass 

    } else {
      line = el.children[0].children[0]

      pos1 = previous__element.children[2].getBoundingClientRect()

      position1 = {
        top: pos1.top,
        left: pos1.left,
      }

      pos2 = el.children[2].getBoundingClientRect()

      console.log(previous__element.children[2])
      console.log(el.children[2])

      position2 = {
        top: pos2.top,
        left: pos2.left,
      }

      line.setAttribute('x1', Math.trunc(pos1.left))
      line.setAttribute('y1', Math.trunc(pos1.top))
      line.setAttribute('x2', Math.trunc(pos1.left))
      line.setAttribute('y2', Math.trunc(pos1.top))
      line.setAttribute('stroke', 'white')
    }
  }

html that gets output:

Text

For some reason this does not actually show the lines, when hovering over them in dev tools it shows the height and width is 0. I'm trying to get the line to connect to the markers in the elements.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

In this example I use position relative/absolute on all the elements. I don't know if that fits your solution, but the core of the example is that the SVG document is in the background of all the boxes. So, all the lines could be placed in that one SVG document.

Maybe the reason why your lines are not showing up is that they mis the stroke-width or that your SVG element does not have a width and a height.

let chart = document.querySelector('#chart');
let poschart = chart.getBoundingClientRect();

let line = chart.querySelector('svg line');
let boxs = chart.querySelectorAll('div.box');

let pos1 = boxs[0].getBoundingClientRect();
line.setAttribute('x1', pos1.x+pos1.width/2-poschart.x);
line.setAttribute('y1', pos1.y+pos1.height/2-poschart.y);

let pos2 = boxs[1].getBoundingClientRect();
line.setAttribute('x2', pos2.x+pos2.width/2-poschart.x);
line.setAttribute('y2', pos2.y+pos2.height/2-poschart.y);
#chart {
  position: relative;
  width: 400px;
  height: 300px;
  margin: 10px 20px;
  border: thin solid black;
}

#chart svg {
  position: absolute;
}

.box {
  position: absolute;
  padding: .5em;
  border: thin solid black;
  background-color: white;
}
<div id="chart">
  <svg viewBox="0 0 400 300" width="100%" height="100%">
    <line stroke="black" stroke-width="2"/>
  </svg>
  <div class="box" style="left:50px;top:180px">Box 1</div>
  <div class="box" style="left:310px;top:100px">Box 2</div>
</div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!