var pieces = document.getElementsByClassName("piece"); var svg = document.getElementById("strings"); for (var i = 0; i < pieces.length - 1; i++) { var a = pieces[i].getBoundingClientRect(); var b = pieces[i + 1].getBoundingClientRect(); var line = document.createElementNS("http://www.w3.org/2000/svg", "line"); line.setAttribute("x1", a.left + a.width / 2); line.setAttribute("y1", a.top + a.height / 2); line.setAttribute("x2", b.left + b.width / 2); line.setAttribute("y2", b.top + b.height / 2); line.setAttribute("stroke", "#b30000"); line.setAttribute("stroke-width", "2"); svg.appendChild(line); }