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

192
Visualizações
Buttons added with JS are not spaced the same as those created with HTML

JavaScript added buttons must be identical to existent pair created with HTML.

function hue() {
  var paragraph = document.createElement("p");

  var button3 = document.createElement("button");
  button3.innerHTML = "Button 3";

  var button4 = document.createElement("button");
  button4.innerHTML = "Button 4";

  paragraph.appendChild(button3);
  paragraph.appendChild(button4);

  var body = document.getElementById("body");
  body.appendChild(paragraph);
}
<body id="body">
  <p>
    <button>
        Button 1
    </button>
    <button>
        Button 2
    </button>
  </p>
  <p>
    <button onclick="hue()">
        Add
    </button>
  </p>
</body>

I've done some research about it and found no explanation. I thank you for a solution and (I'd be a bonus) an explanation why this occur.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The buttons you're creating in the HTML has whitespace between them. I changed your fiddle to:

        <button>Button 1</button><button>Button 2</button>

and they ended up identical.

about 4 years ago · Juan Pablo Isaza Relatório

0

The difference is that your HTML contains whitespace (the new line) in between your button elements, whereas the JavaScript DOM methods you're using do not add this whitespace. You can see removing the whitespace in the markup yields the same results:

function hue() {
  var paragraph = document.createElement("p");

  var button3 = document.createElement("button");
  button3.innerHTML = "Button 3";

  var button4 = document.createElement("button");
  button4.innerHTML = "Button 4";

  paragraph.appendChild(button3);
  paragraph.appendChild(button4);

  var body = document.getElementById("body");
  body.appendChild(paragraph);
}
<body id="body">
  <p>
    <button>Button 1</button><button>Button 2</button>
  </p>
  <p>
    <button onclick="hue()">Add</button>
  </p>
</body>

If you want the buttons to be spaced apart a certain way I strongly recommend you style them the way you want:

function hue() {
  var paragraph = document.createElement("p");

  var button3 = document.createElement("button");
  button3.innerHTML = "Button 3";

  var button4 = document.createElement("button");
  button4.innerHTML = "Button 4";

  paragraph.appendChild(button3);
  paragraph.appendChild(button4);

  var body = document.querySelector("body");
  body.appendChild(paragraph);
}
p {
  display: flex;
}

button {
  margin-inline: .2em;
}
<p>
  <button>Button 1</button>
  <button>Button 2</button>
</p>
<p>
  <button onclick="hue()">Add</button>
</p>

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