Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
Undefined is not an object?
I'm trying to append 'target="_blank"' to all links with the class external using only jQuery. Here is my code:
I get the error Undefined is not an object. What is causing this?
Kevin Korte28,135 Points
Your jquery code is looking for an element with the class of external, with an attribute of a. There is no element with an a attribute, so it comes back undefined. I don't even think there is a valid a attribute in html.
Undefined, means it can't find it, it doesn't exist.
Using the jquery documentation, since we know we want to add the attribute
target="_blank", than let's take a look. We're going to jump halfway down to here: http://api.jquery.com/attr/#attr2 where it shows us how to use
.attr() to add an attribute.
As you can see, it's the very first one is what we need, where it says
.attr( attributeName, value ). Perfect.
So going back to the question, it's just ends up being simply `$('.external').attr('target', '_blank');'
And boom, that's it.
Jennifer NordellTreehouse Teacher
Hi there! This question seems to throw some people and most seem to think they need the append method here. This is not the case. The only method you need here is
.attr(). And that method takes two strings. The first one is the attribute to be changed (which you did correctly) and the second is the value of that attribute (which you also did correctly). In fact, if I just selectively delete a little of your code, it passes! So give it another shot with these hints in mind!