Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Leonard Morrison](https://uploads.teamtreehouse.com/production/profile-photos/6110392/micro_captured_photo1455868465139.jpg)
Leonard Morrison
Courses Plus Student 32,914 PointsI'm running into some trouble here.
Apparently, I keep running into this issue: TypeError: $image.attr is not a function; (I use a Google App that detects JS errors).
Anyways, I can't seem to figure out why this seems to be an issue.
Snapshot of the workspace: https://w.trhou.se/sodnus4q32
In case you need the code immediately:
//Problem: user when clicking on image goes to dead end.
//Solution: Make a matchbox for the gallery
var $overlay = $('<div id="overlay"></div>');
var $image = ('<img>');
$overlay.append($image);
//$("body").append($overlay);
//1) Capture the click event on a link to an image.
$("#imageGallery a").click(function(event){
event.preventDefault();
var imageLocation = $(this).attr('href');
console.log(imageLocation);
$image.attr("src", imageLocation);
//^ Here's the apparent problem.
$overlay.show();
});
$overlay.click(function()
{
$overlay.hide();
})
body {
font-family: sans-serif;
background: #384047;
}
h1 {
color: #fff;
text-align: center
}
ul {
list-style:none;
margin: 0 auto;
padding: 0;
display: block;
max-width: 780px;
text-align: center;
}
ul li {
display: inline-block;
padding: 8px;
background:white;
margin:10px;
}
ul li img {
display: block;
}
a {
text-decoration: none;
}
/** Start Coding Here **/
#overlay
{
background:rgba(0,0,0,0.7);
width::100%;
height: 100%;
position:absolute;
top:0;
left:0;
display:none;
text-align:center;
}
#overlay img
{
margin-top:10%;
}
1 Answer
![Steven Parker](https://uploads.teamtreehouse.com/production/profile-photos/969492/micro_tool_chess_set_2.jpg)
Steven Parker
230,688 PointsWhat Jennifer is calling the "alternative" suggestion is correct.
Jennifer's "alternate" suggestion was to add a $ before the parentheses on this line:
var $image = $('<img>');
The first suggestion (which was: "
$($image).attr("src", imageLocation);
") will not work because the JQuery object it sets the attribute on will not be attached to the document.
And putting a $ in front of a variable name has no special meaning to JavaScript. But it is a common convention to name variables that will be used to hold JQuery objects this way, since the $ itself is associated with JQuery. It just makes reading the code easier.
Jennifer Nordell
Treehouse TeacherJennifer Nordell
Treehouse TeacherOk, I guess I'm wrong, but I sincerely don't understand why. When I try it, it works. What am I missing?
Steven Parker
230,688 PointsSteven Parker
230,688 PointsWhen you say "it works", you probably mean it doesn't cause an error.
But it also doesn't change the attribute on the image inside the overlay that was attached to the document, which is the purpose of that line of code. The object it sets the attribute on is created by that line, and not assigned to a variabe or appended to the document, so it cannot be referenced later. It will exist "in limbo".![:wink: :wink:](https://image-proxy-cdn.teamtreehouse.com/2e760b3ad2356c698f6445aa940a24edfd675455/68747470733a2f2f7374617469632e7465616d74726565686f7573652e636f6d2f696d616765732f656d6f6a692f756e69636f64652f31663630392e706e67)
Jennifer Nordell
Treehouse TeacherJennifer Nordell
Treehouse TeacherSteven Parker I think I see what you mean now. Thanks for the correction. I will be removing my answers.
Steven Parker
230,688 PointsSteven Parker
230,688 PointsOK, but now I have to edit mine so it makes sense again.![:stuck_out_tongue_winking_eye: :stuck_out_tongue_winking_eye:](https://image-proxy-cdn.teamtreehouse.com/5f2215b87833561b28b25d1e13b8f6d692e0904d/68747470733a2f2f7374617469632e7465616d74726565686f7573652e636f6d2f696d616765732f656d6f6a692f756e69636f64652f31663631632e706e67)
Jennifer Nordell
Treehouse TeacherJennifer Nordell
Treehouse TeacherSteven Parker Sorry![:cry: :cry:](https://image-proxy-cdn.teamtreehouse.com/870f095e8778cecaae927e2cc13c408f0b7e767a/68747470733a2f2f7374617469632e7465616d74726565686f7573652e636f6d2f696d616765732f656d6f6a692f756e69636f64652f31663632322e706e67)
Steven Parker
230,688 PointsSteven Parker
230,688 PointsYou're probably just tired — your analysis is normally spot-on. And you did get it right the 2nd time.![:+1: :+1:](https://image-proxy-cdn.teamtreehouse.com/6d8de85e38fee3161a0f29fbc06cd3701ecdafed/68747470733a2f2f7374617469632e7465616d74726565686f7573652e636f6d2f696d616765732f656d6f6a692f756e69636f64652f31663434642e706e67)
Leonard Morrison
Courses Plus Student 32,914 PointsLeonard Morrison
Courses Plus Student 32,914 PointsThank you for loading the code. It was a pain to keep the code from a window loaded before and look at the current window.