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()

jQuery Challenge - I'm not even sure what they are asking for

This is the challenge:

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

I have tried a couple of things as pure guesses. It boils down to the fact that I am not even sure what the challenge is. I think my biggest problem is I don't know what is meant by "target attribute".

3 Answers

What is asking is to add a new attribute with the name of target and the value of '_blank' to every tag that has the class of external. So there is two anchor tags with the class of external. You just add target='_blank' within the tag.

    <li><a href="http://google.com" class="external">Google</a></li>
    <li><a href="http://yahoo.com" class="external">Yahoo</a></li>

to

    <li><a href="http://google.com" class="external" target='_blank'>Google</a></li>
    <li><a href="http://yahoo.com" class="external" target='_blank'>Yahoo</a></li>

Doing this in JQuery looks like

$('.external').attr('target', '_black');
Jesus Bayona
Jesus Bayona
4,937 Points

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

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

$("a.external").attr('target' ,'_blank'); Is selecting all anchor tag with the class of external. In this example either would work it doesn't change the end result. So if had a paragraph tag in the html with the class of external <p class='external'> ... </p>

$("a.external").attr('target' ,'_blank'); wouldn't select the new p tag. $(".external").attr('target' ,'_blank'); would select the new p tag

Hey Doug,

Seems like someone had a similar problem a couple months back. Here's the link to their thread.

Hope this helps!

Donald:

That was simple. There was just something clumsy in the way the challenge was specified that caused me not to "connect". Thanks for the help.