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

218
Vistas
Moving a button that has already been created with JavaScript

Here I tried to create a JS function for creating buttons, which I have done so successfully. What hasn't worked for me, is finding a way to move/position that button within the same function. I scoured the Internet for an answer, but couldn't find one.

function createButtons(dialogueString, buttonIdString) {
    var btn = document.createElement("button");
    btn.style.left = "50%"; //here is one of my attempts to move the button but it is not working
    btn.id = buttonIdString;
    btn.innerHTML = dialogueString;
    document.body.appendChild(btn);
};
createButtons("Hello", "choice1");

As you can see above, I tried using btn.style.left to move the button to the left, I tried writing "200px", instead of "50%" and many other stuff, but nothing has worked. If you have any idea how to solve this, please let me know, I would really appreciate it.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

just add btn.style.position = 'absolute'; or btn.style.position = 'relativ';to your function

function createButtons(dialogueString, buttonIdString) {
    var btn = document.createElement("button");
    btn.style.left = "50%"; //here is one of my attempts to move the button but it is not working
    btn.style.position = 'absolute';
    btn.id = buttonIdString;
    btn.innerHTML = dialogueString;
    document.body.appendChild(btn);
};
createButtons("Hello", "choice1");

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can make css class with desired style and include it in js.

For example:

//style.css
.buttonStyle{
  color: red;
}

//javascript part
btn.classList.add("buttonStyle");
about 4 years ago · Juan Pablo Isaza Denunciar

0

Found the solution. I did btn.style.marginLeft = "+50px" and it seems to work.

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