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 trialMatthew Lehman
5,282 PointsUsing jQuery only, add to all links with the class "external", the target attribute with the value of "_blank".
$external.attr("external", _blank);
8 Answers
Chyno Deluxe
16,936 PointsThe challenge states for you to target all links with the class "external". To target an element with a class you your do the following.
$("a.external").attr('target' ,'_blank');
I hope this helps
Prashant chaudhari
Courses Plus Student 24,499 Pointsthank a lot.
amadeo brands
15,375 PointsThak you great it worked for me
Joe Gorsick
14,313 PointsThis is what I came up with on my own and it wouldn't work: $('.external').attr('target', '_blank'); I had to add the a. before external, or it wouldn't work.
Akbar Khatibi
8,137 Pointswhy is there an A before external? and when it says "the target attribute with a value of '_blank'", what exactly is target and what exactly is _blank and why does it need to be together like that in the attribute function?
Lincoln Wisely
7,481 Points@Akbar –
The target="_blank" attribute causes the linked URL to open in a new tab.
Example: <a href="http://teamtreehouse.com" target="_blank">Team Treehouse</a>
So for this exercise, you first add the attribute "target". And then add the value of "_blank".
.attr("target","_blank")
As for your first question – "a.external" is selecting all links (a) with the class .external. It's different from the video, which taught a "class/id (space) element" model. (E.g., ".external a").
Hope this helps!
Dominic Serrano
6,340 PointsSo the following code:
Ex: $('.external a').attr('target', '_blank')
would not select all links with the class 'external'?
Only: $('a.external').attr('target', '_blank')
would be correct for selecting all links?
Chyno Deluxe
16,936 PointsThat is correct. Here's the html equivalent of what both selectors do.
<!-- $('.external a') -->
<!-- this selector will target all a tags inside the element with the class .external -->
<div class="external">
<a class="google" href="http://google.com">Google</a>
<a class="google1" href="http://google.com">Google</a>
<a class="google2" href="http://google.com">Google</a>
<a id="google" href="http://google.com">Google</a>
</div>
<!-- $('a.external') -->
<!-- this selector will target all a tags inside the element with the class .external -->
<div class="external"> <!-- will not target this element because it is not an a tag -->
<a class="external" href="http://google.com">Google</a> <!-- will target this element -->
<a class="google1" href="http://google.com">Google</a> <!-- will not target this element because it does not have class of .external -->
<a class="external" href="http://google.com">Google</a> <!-- will target this element -->
<a id="google" href="http://google.com">Google</a> <!-- will not target this element because it does not have class of .external -->
</div>
Brandon Miller
6,818 PointsThis thread was very helpful. jquery makes more sense now and is easier than i thought.
Chyno Deluxe
16,936 PointsGlad you were able to find this information useful.
Ramon Dharmawan
Courses Plus Student 2,651 Pointshi,Chyno. Thanks for the explaination, it helping a lot.
Matthew Lehman
5,282 PointsMatthew Lehman
5,282 PointsThanks a lot.
I over thought the question
I knew that the "a.external" was the key to link to all targets.
Jesus Bayona
4,937 PointsJesus Bayona
4,937 PointsI seen this in another post concerning this same question and it still worked. What's the difference?
$('.external').attr('target', '_blank');
Chyno Deluxe
16,936 PointsChyno Deluxe
16,936 PointsJesus Bayona , There is no difference other than specificity.
The answer i provided above specifically targets the a tag and no other elements with the class .external unlike your answer.
Akbar Khatibi
8,137 PointsAkbar Khatibi
8,137 Pointswhy is there an A before external? and when it says "the target attribute with a value of '_blank'", what exactly is target and what exactly is _blank and why does it need to be together like that in the attribute function?
Chyno Deluxe
16,936 PointsChyno Deluxe
16,936 PointsAkbar, The a is targeting all a tags. So what a.external is doing is targeting all a tags with the class of external.
The a tag has an attribute called target which lets the browser know how to handle the link. the _blank target tells the browser to open the link on a separate browser tab.
You can find more information about the target attribute at W3Schools Website.
I hope this helps.
rahul dravid
Courses Plus Student 234 Pointsrahul dravid
Courses Plus Student 234 PointsThanks man this is really helpful
Ramon Dharmawan
Courses Plus Student 2,651 PointsRamon Dharmawan
Courses Plus Student 2,651 PointsHello, first i put this, $("a .external").attr('target' ,'_blank'); , so i put a space betwen a and .external. and it said " I was expecting
target
to be_blank
not `` " In the video, i found a code written like this " $("#imageGallery a")... " My question is, $("a .external") is not the same like $("#imageGallery a") ? what is happening ? mybe Somebody can explain me?. Thanks in advanceChyno Deluxe
16,936 PointsChyno Deluxe
16,936 PointsRamon Dharmawan The two selectors you mentioned are not the same.
In regards to this challenge,
$('a .external')
and$('a.external')
are two very different selectors.I hope this helps answer your question.