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

195
Views
Can't programmatically add <line> element inside <g> element

I am trying to create a <line> element and append it to a <g> element. When I do it through HTML, it works:

    <svg>
      <g id="graph-paper">
        <line class="lineStyle" x1="0" y1="0" x2="100" y2="100"></line>
      </g>
    </svg>

    <style>
       .lineStyle {
         fill: none;
         shape-rendering: crispEdges;
         stroke: black;
         stroke-width: 1px;
       }
    </style>

But when I try to add it with javascript, it doesn't:

let graphPaper = document.getElementById("graph-paper")
let line = document.createElement("line")
line.setAttribute("x1", "0")
line.setAttribute("x2", "20")
line.setAttribute("y1", "40")
line.setAttribute("y2", "80")
line.setAttribute("class", "lineStyle")
graphPaper.appendChild(line)

What am I doing wrong? I want to do it with javascript.

about 4 years ago · Juan Pablo Isaza
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!