Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

193
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda