Tiggles ツ of Speak Creative
11/28/2018 - 5:38 PM

bg-img.js

find an image contained within markup tree, remove said image, and apply it to the parent that has the bg-img class

$(".bg-img").each(function() {
	var img = $(this).find("img");
	$(this).css({
		"background-image": "url(" + img.attr("src") + ")",
		"background-position": "center top",
		"background-size": "cover",
		"background-repeat": "no-repeat",
		"width": "100%"
	});
	img.remove();
});


// or

$(".bg-img").each(function() {
  var img = $(this).find("img")[0];
  if (img !== undefined && img.getAttribute("src") !== undefined) {
    $(this).css({
      "background-image": "url(" + img.getAttribute("src") + ")"
    });
    img.parentNode.removeChild(img);
  }
});