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

JavaScript

attr('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.

alt text alt text

1 Answer

I 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?

Thank 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

The 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.

I did it. Haha. I make a careless mistake.

Thank you very much, Maciej Czuchnowski.