Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

172
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda