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

184
Vistas
how would I keep my button from moving whenever I press it and change the margin

I am trying to keep my button from moving to the right whenever I press the button and change the margin of the page I am on. I am doing this to kinda simulate window resizing.

Here is a code-snippet of the current code:

const button = document.createElement('button');
button.setAttribute("id", "1st");
button.innerText = "BRANCH";
button.style.color = 'red';
button.style.position = 'absolute';

button.style.zIndex = 0;
button.style.width = '16%';
button.style.height = '10%';

button.style.left = '45%';
button.style.bottom = '30%';
button.style.right = '0%';
button.style.top = '29%';
button.style.padding = '2% 3%';

document.querySelector('body').append(button);

button.addEventListener('click', () => {
  console.log("button pressed")

  var x = document.createElement("P");
  x.innerText = "THIS IS A TEST";
  x.style.marginLeft = "100%";
  x.style.zIndex = -1;

  document.querySelector('div').appendChild(x);

});

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

0

Cause of Problem here is document.querySelector()

According to developer.mozilla.org

The Document method querySelector() returns the first Element within the document that matches the specified selector, or group of selectors. If no matches are found, null is returned."

problem is when you use document.querySelector('div') it finds the "first" div in your code.

in here document.querySelector('div') might have found the div your button is also in. Thus onclick 100% of margin left paragraph (<p></p>) tag is added to your document.

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