Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

176
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda