Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript jQuery Basics (2014) Creating a Simple Lightbox 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".

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

js/app.js

index.html
<!DOCTYPE html>
<html>
<head>
  <link rel="stylesheet" href="css/style.css" type="text/css" media="screen" title="no title" charset="utf-8">
  <title>Links Page</title>
</head>
<body>
  <h1>Links</h1>
  <ul>
    <li><a href="http://google.com" class="external">Google</a></li>
    <li><a href="http://yahoo.com" class="external">Yahoo</a></li>
  </ul>
  <script src="//code.jquery.com/jquery-1.11.0.min.js" type="text/javascript" charset="utf-8"></script>
  <script src="js/app.js" type="text/javascript" charset="utf-8"></script>
</body>
</html>

4 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! I really wish I knew what you'd tried already that's failing. I believe it would be a better learning opportunity for you if I could say where you're going wrong or what you're not understanding. I'll show you my solution and walk you through it.

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

Here we first select the class "external". We can tell this is a class and not an id as it is preceded with a period. Then we use attr on our selected class. This method takes two strings: the attribute to be set and the value it is to be set to. The result of this code will be that all links that have the class "external" will be opened in a new window/tab when clicked. Happy coding! :sparkles:

Steve Linn
Steve Linn
11,841 Points

why is it it that you do not have to specify the link attribute of a?

$(".external a").attr("target","_blank");
Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Because if you look at the HTML you'll notice that only links have the class of external :sparkles: So it's not necessary.

Steve Linn
Steve Linn
11,841 Points

so is it semantically incorrect - meaning it would throw an error? or just the quiz example. I lean on the side of more specificity

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Actually, I retract my previous comment. After re-reading the challenge it says explicitly to target all links with the class "external". So besides my original answer you could also do this one:

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

This will select all links with the class "external". There's a subtle but important difference between this code and yours. Your code selects all links within the class "external". Let's say for example that these links have no class assigned but they reside inside a div with the class "external". Your code would select them.

But let's say we have a div with no class, but the links have the class "external". That's what this code selects.

Hope this helps! :sparkles:

Steve Linn
Steve Linn
11,841 Points

Oh right! the specificity of the a.external --- good catch!