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

Does anyone understand the code chalenge "adding new attribute values with attr()" ?

" Using jQuery only, add to all links with the class "external", the target attribute with the value of "_blank" " from stage 3 Jquery basics.

Maybe I didn`t anderstand it at all. The 'target' attribute appear only with 'event'? how am I suppose to use it?

thanks...

6 Answers

Try

$('a').attr('target', "_blank");
$('a').addClass('external');

I don't know much jquery, but that seems to add a class to anchors and add the target. The target attribute dictates whether the link will open in a new tab, the current window etc.

(update: seems i misread the request. oops)

jQuery allows chaining, so:

$('a').attr('target', '_blank').addClass('external');

The target attribute is a valid HTML5 attribute on <a> elements which specifies what context to open that link's href attribute value in. A target of _blank forces the link to open in a new window rather than the default behavior of the same window.

mm All I can think of is doing this or something like it,

$('a.external').attr('target', '_blank');

Because it's saying that for all links that have the class "external", add the value "_blank" to the target attribute. Whereas your solution is targeting all anchor elements, and also adding the class external to the anchor elements, in the last chain.

Hope that helps.

If it's "For all links (<a>) that have the class external (<a class="external">), add the value "_blank" to the target (<a class="external" target="_blank"), then the solution is:

$('a.external').attr('target', '_blank');

Sorry, wasn't understanding you there.

In short, $('a.external') has jQuery find all <a> elements on the page that have a class attribute which contains external. The when we chain the .attr() function, it iterates over each <a> element it found, and changes that element's target attribute key to a value of _blank. That sounds like what you're wanting. I always say it's better not to just be given an answer, but to be explained the process so you can comprehend what is going on.

It's pretty much what I wrote above your comments haha, but I guess the more explanations, the better xD

You're right Javier. I passed right over it because I didn't see the styling. For clarity sake, you can press the backtick button (on the same button as the ~, but without the SHIFT) 3 times, then type the word javascript right next to it, make a new line, enter your javascript, make a last new line and do 3 more backticks to close it off. That will auto-format your code to JS for easier readability.

// like this!

aha! that's how you do it. Thanks, here's a whale for the sake of a lazy monday.

Edit: Well it won't format to show the whale :D

thank you all very much!!! you helped me a lot!