Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

181
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!