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

173
Visualizações
How to assign background image to html div using javascript?

Can someone explain this to me:

I have a div which background image is working:

<section class="bottom-banner" id="imagebannerthree" style="background-image:url(https://*****.com.au/wp-content/custom-products/1628/bottom-3.jpg);">
</section>

but when i tried loading background image using javascript there is no image loaded:

document.getElementById('imagebannerthree').style.backgroundImage = checkImage("https://*****.com.au/wp-content/custom-products/1628/bottom-3.jpg");

 function checkImage(url) {
    var http = new XMLHttpRequest();
    http.open('HEAD', url, false);
    http.send();

    if (http.status == 200) {
        return 'url(' + url + ')';
    } else {
      document.getElementById('' + parentdiv).remove();
    }
  }
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You need to use quotes while assigning value to style.backgroudImage

document.getElementById('imagebannerthree').style.backgroundImage = "url('https://*****.com.au/wp-content/custom-products/1628/bottom-3.jpg')";

you most probably getting the error "url is not a function".

about 4 years ago · Juan Pablo Isaza Relatório

0

document.getElementById('imagebanner').style.backgroundImage = "url(https://piranhatc.com.au/wp-content/custom-products/1628/bottom-3.jpg)";

 function checkImage(url) {
    var http = new XMLHttpRequest();
    http.open('HEAD', url, false);
    http.send();

    if (http.status == 200) {
        return 'url(' + url + ')';
    } else {
      document.getElementById('' + parentdiv).remove();
    }
  }
.bottom-banner {
  object-fit: cover; 
  width: 200px;
  height: 200px;
}
<section class="bottom-banner" id="imagebanner">
</section>

name in id does not match. The functions written below seem irrelevant.

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