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

271
Views
edit 'value px' in elemtn.style.width px using Javascript

I got a function which gets the size from the user and creates the div, but I'm not sure how to express the size in front of px.

function createDiv(size) {
  const div = document.createElement("div");
  div.classList.add("box");
  div.style.width = `${size} px`;
  div.style.height = `${size} px`;
  console.log(size);

  return div;
}

It works if I input the size as 75px:

function createDiv(size) {
  const div = document.createElement("div");
  div.classList.add("box");
  div.style.width = "75px";
  div.style.height = "75px";
  console.log(size);

  return div;
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You need to remove the space between the value and px, it should be like this: ${size}px.

function createDiv(size) {
  const div = document.createElement("div");
  div.classList.add("box");
  div.style.width = `${size}px`;
  div.style.height = `${size}px`;
  document.body.appendChild(div);
}

createDiv(100);
.box {
  background: lawngreen;
}

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!