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 jQuery Basics (2014) Creating a Simple Lightbox Adding New Attribute Values with attr()

Matthew Lehman
Matthew Lehman
5,282 Points

Using jQuery only, add to all links with the class "external", the target attribute with the value of "_blank".

$external.attr("external", _blank);

8 Answers

The 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

Matthew Lehman
Matthew Lehman
5,282 Points

Thanks a lot.

I over thought the question

I knew that the "a.external" was the key to link to all targets.

Jesus Bayona
Jesus Bayona
4,937 Points

I seen this in another post concerning this same question and it still worked. What's the difference?

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

Jesus 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
Akbar Khatibi
8,137 Points

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

Akbar, 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.

Thanks man this is really helpful

Ramon Dharmawan
Ramon Dharmawan
Courses Plus Student 2,651 Points

Hello, 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 advance

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

$('a .external') // targets any element INSIDE of an "a" tag with the class external
$('a.external') // targets any "a" tag with the class of external
<a>
  <span class="external">$('a .external') will target this element</span>
</a>

<a class="external">$('a.external') will target this element</a>

I hope this helps answer your question.

thank a lot.

amadeo brands
amadeo brands
15,375 Points

Thak you great it worked for me

This 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
Akbar Khatibi
8,137 Points

why 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
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
Dominic Serrano
6,340 Points

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

That 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
Brandon Miller
6,818 Points

This thread was very helpful. jquery makes more sense now and is easier than i thought.

Glad you were able to find this information useful.