When I hover over a picture on this page, I should be updating the larger div element’s src attribute above to be the image url of the image I am currently hovering over.
My breakpoints reach up to the
“$(‘#image’).on(‘hover’, function() {“
line, but won’t actually set the url attribute of the div element on the next line. Any pointers?
JavaScript
x
6
1
function upDate(previewPic) {
2
let newUrl = previewPic.src;
3
$('#image').on('hover', function() {
4
$('#image').attr("url", newUrl);
5
});
6
};
JavaScript
1
11
11
1
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
2
3
<div id="image">
4
Hover over an image below to display here.
5
</div>
6
7
<img alt="Batter is ready" class="preview" onmouseout="unDo()" onmouseover="upDate(this)" src="https://cdn.sallysbakingaddiction.com/wp-content/uploads/2017/06/moist-chocolate-cupcakes-7.jpg">
8
9
<img alt="Perfect Baking" class="preview" onmouseout="unDo()" onmouseover="upDate(this)" src="https://cdn.sallysbakingaddiction.com/wp-content/uploads/2017/06/moist-chocolate-cupcakes-6.jpg">
10
11
<img alt="Yummy yummy cup cake" class="preview" onmouseout="unDo()" onmouseover="upDate(this)" src="https://cdn.sallysbakingaddiction.com/wp-content/uploads/2017/06/moist-chocolate-cupcakes-5.jpg">
Advertisement
Answer
- Remove all inline event handlers
- Add mouseenter and leave handlers
- Access the css property
Divs do not have URLs
Also I moved the preview to not have to scroll too far down
JavaScript
1
6
1
$(".preview").on("mouseenter",function() {
2
$("#image").css({"background-image": `url(${this.src})`}); // this.src is the DOM notation for the source of the image you are hovering
3
})
4
$(".preview").on("mouseleave",function() {
5
$("#image").css({"background-image": "" })
6
})
JavaScript
1
3
1
#image {
2
3
height: 500px }
JavaScript
1
12
12
1
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
2
3
4
<img alt="Batter is ready" class="preview" src="https://cdn.sallysbakingaddiction.com/wp-content/uploads/2017/06/moist-chocolate-cupcakes-7.jpg" height="50">
5
6
<img alt="Perfect Baking" class="preview" src="https://cdn.sallysbakingaddiction.com/wp-content/uploads/2017/06/moist-chocolate-cupcakes-6.jpg" height="50">
7
8
<img alt="Yummy yummy cup cake" class="preview" src="https://cdn.sallysbakingaddiction.com/wp-content/uploads/2017/06/moist-chocolate-cupcakes-5.jpg" height="50">
9
10
<div id="image">
11
Hover over an image above to display here.<br/>
12
</div>