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

David Dehghani
David Dehghani
6,992 Points

Not undering standing the attr() in jQuery. Help!

I don't think I'm retrieving the correct information for the exercise. Tried a few different things, but obviously it's not correct.

js/app.js
var target = "_blank";
$(".external, a").attr("href", target);
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>

2 Answers

Dan Jordan
Dan Jordan
7,883 Points

In this exercise, you to not need to assign a value to a variable. Here, your syntax is incorrect, and above, you are targeting the a tag (child) within the class .external (parent), when you want to target all a tags with the class .external. Your code should look something like this:

$('.external').attr('target','the_value_i_am_setting_to_target');
Dan Jordan
Dan Jordan
7,883 Points

.attr() allows you to either get or set attributes. In your example above, you are setting the target attribute of .external a to "_blank". If you wish to get the target attribute, your code would look like this:

var target = $(".external a").attr("href");  // no comma

If the a tag has a class of external, it would look like this:

var target = $("a.external").attr("href");

Hope this helps!

David Dehghani
David Dehghani
6,992 Points

It still doesn't seem to be passing. I even tried:

var target = $(".external" a).attr("href");