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
Noga Anaby
Courses Plus Student 4,255 PointsDoes 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
Simon Coates
28,695 PointsTry
$('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)
Aaron Martone
3,290 PointsjQuery 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.
Javier Mera
16,472 Pointsmm 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.
Aaron Martone
3,290 PointsIf 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.
Aaron Martone
3,290 PointsIn 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.
Javier Mera
16,472 PointsIt's pretty much what I wrote above your comments haha, but I guess the more explanations, the better xD
Aaron Martone
3,290 PointsYou'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!
Javier Mera
16,472 Pointsaha! 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
Noga Anaby
Courses Plus Student 4,255 Pointsthank you all very much!!! you helped me a lot!