I've been attempting to modify the background of a div in HTML using JS and CSS. Below is the code I've tried, but it doesn't seem to be working as expected.
function changeBackground(previewPic)
{
var imgUrl = previewPic.src;
console.log(imgUrl);
x = document.getElementById('image');
document.getElementById('1').style.backgroundImage = previewPic.src;
}
body{
margin: 2%;
border: 1px solid black;
background-color: #b3b3b3;
}
.image{
line-height:650px;
width: 575px;
height: 650px;
border:5px solid black;
margin:0 auto;
background-color: #8e68ff;
background-image: url('');
background-repeat: no-repeat;
color:#FFFFFF;
text-align: center;
background-size: 100%;
margin-bottom:25px;
font-size: 150%;
}
.preview{
width:10%;
margin-left:17%;
border: 10px solid black;
}
img{
width:95%;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Photo Gallery</title>
<link rel="stylesheet" href="css/style.css">
<script src = "js/gallery.js"></script>
</head>
<body>
<div class="image" id = "1">
Hover over an image below to display here.
</div>
<img class = "preview" alt = "Styling with a Bandana" src = "https://s3-us-west-2.amazonaws.com/s.cdpn.io/389177/bacon.jpg" onmouseover = "changeBackground(this)" onmouseout = "resetBackground()">
<img class = "preview" alt = "With My Boy" src = "https://s3-us-west-2.amazonaws.com/s.cdpn.io/389177/bacon2.JPG" onmouseover = "changeBackground(this)" onmouseout = "resetBackground()">
<img class = "preview" src = "https://s3-us-west-2.amazonaws.com/s.cdpn.io/389177/bacon3.jpg" alt = "Young Puppy" onmouseover = "changeBackground(this)" onmouseout = "resetBackground()">
</body>
</html>
This function retrieves the image source URL and stores it in the 'imgUrl' variable. However, despite logging the URL correctly, the background update is not functioning as intended.