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

175
Visualizações
How to Add Element Created in JavaScript File to HTML page

I am attempting a create an element in the js file which will then be added to the html div where the game is

I am trying to make a createMine function that creates a new element with the style in .mine and then adds it the the html game div.

var newMine = document.createElement("div");
newMine.classList.add(".mine");
document.getElementById("game").appendChild(newMine);
.mine {
  width: 20px;
  height: 20px;
  background-color: blue;
  position: relative;
  display: inline;
}
<div id="game">
  <button id="startButton">Click To Start</button>
  <div id="gameElements">
    <div id="character">
    </div>
  </div>
</div>

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

0

Your code is working properly except two problems:

  1. newMine.classList.add(".mine"); should be newMine.classList.add("mine"); (don't use the . when adding the class, its only purpose is to tell CSS that the following is a CSS class).

  2. Elements that have display: inline don't accept height or width. Use display: inline-block instead.

about 4 years ago · Juan Pablo Isaza Relatório

0

var newMine = document.createElement("div");
newMine.classList.add("mine");
document.getElementById("game").appendChild(newMine);
.mine{
    width: 20px;
    height: 20px;
    background-color: blue;
    position: relative;
    display: inline-block;
}
<div id="game">
    <button id="startButton">Click To Start</button>
    <div id="gameElements">
        <div id="character"></div>
    </div>
</div>

when using classlist, don't write a dot at the beginning to make it work. and as @connexo said, use inline-block

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