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

Marcus Schumacher
Marcus Schumacher
16,616 Points

Need help with this J Query challenge

Instructions are:

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

I have no idea what to do here. Please help. Thanks.

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>

3 Answers

Tushar Singh
PLUS
Tushar Singh
Courses Plus Student 8,692 Points

Whenever you have to change the attribute or set the attribute, use "attr".

"target"---- attribute to target "_blank"--- set the value of that attribute

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

It is asking you to target all anchor elements ("a") with the class "external," like so:

$("a.external").attr('target' ,'_blank');
Brad Sprague
Brad Sprague
5,523 Points

The phrasing of the question is confusing. to me I poked around long enough to guess the answer. In case it is a murky to someone else, here is what it is asking:

  1. "add to all links": This means work with <a> elements.

  2. 'with the class "external" ': That gets us to a.external.

  3. "Using jquery only," we find all such elements like this: $("a.external")

  4. "The target attribute." As in the video, we can add/access the attributes like this: $("a.external").attr(). Now the name of the attribute is passed-in as a string, so we need this code: $("a.external").attr('target', )

  5. Finally, "the value of "_blank". The English grammar is ambiguous. It either means "the literal value (which is) '_blank' " (meaning use a string literal). Or the author might have meant the value stored in the variable whose name is "_blank". I don't know how to make the determination except to try both and see which passes inspection. It turns out enclosing "_blank" in quotation marks passes, so it is a string literal, not a variable.