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

115
Visualizações
Adding array result to a string

This is a little script randomizing what picture should it display. I can easily set this to an <img> using document.getElementById("pic").src = randPics[randNumGen], but the background tag requires me to use url(). How can I set document.body.style.background to "url(and the random image here)"? Please no JS libraries.

const randPics = ["https://i.ibb.co/6JBbMYm/pic1.jpg", "https://i.ibb.co/rHjTdfc/pic2.jpg"];

window.onload = choosePicture();

function choosePicture() {
  let randNumGen = Math.floor(Math.random() * randPics.length);
  console.log(randNumGen);
  document.body.style.background = "randPics[randNumGen]";
}
<!DOCTYPE html>
<html>

<head>
  <link rel="stylesheet" href="styles.css">
  <script async src="index.js" type="text/javascript"></script>
</head>

<body>
  <img width="500px" src="#" alt="pic" id="pic">
</body>

</html>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

var url = randPics[randNumGen];

document.body.style.backgroundImage = "url('"+ url + "')";
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