Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

273
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda