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
Jia Jing Loh
9,784 Pointsattr('alt') didn't work!
I am playing around with attr(). It worked fine with attr('href') as shown in the video. But when I tried attr('alt'), it showed 'undefined' in JavaScript console.
1 Answer
Maciej Czuchnowski
36,441 PointsI tried this in the workspace, it works perfectly fine. I had an image like this:
<img src="images/refferal_machine.png" width="100" alt="Refferal Machine By Matthew Spiel">
and my jQuery looked like this:
$("img").on("click", function() {
var alter = $(this).attr("alt");
alert(alter);
});
It properly displayed a popup saying "Refferal Machine By Matthew Spiel". Maybe you are missing the alt attribute or using the wrong selector?

Jia Jing Loh
9,784 PointsJia Jing Loh
9,784 PointsThank you for the answer. I wish I can post the image to show you. How can I upload the image like you did?
https://drive.google.com/open?id=0B77aWfPaPphHcjExZTM3cENEdGc&authuser=0 https://drive.google.com/open?id=0B77aWfPaPphHWGJwcVBNb2MxTkE&authuser=0
Maciej Czuchnowski
36,441 PointsMaciej Czuchnowski
36,441 PointsThe image is not important. It can be any image. You have to set the alt attribute in the HTML markup to access it with jQuery.
Jia Jing Loh
9,784 PointsJia Jing Loh
9,784 PointsI did it. Haha. I make a careless mistake.
Thank you very much, Maciej Czuchnowski.