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 trialUnsubscribed User
8,900 PointsHow do I assign a value to an attribute? Or what am I doing wrong??
I cant figure this one out, I don't think I fully understand what the question is asking. It asks to add too all links with the class of "external", the "target" attribute with the value of "_blank" Please help!
Thank you!
var $target = ('_blank');
$('.external').attr($target);
<!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
Erik Nuber
20,629 PointsThe attr() is both a getter and a setter. When you are setting it, you use .attr( attributeName, value )
$('.external').attr("target", "_blank");
The first part as you already know is grabbing the class external. Next we set the attribute by giving it the attribute name to set which is target and, then the value we want it to equal which is _blank.
Karolin Rafalski
11,368 Pointsthe attr() function works both as a getter and a setter. If you want to get an element with a specific attribute one argument is sufficient. However if you want to set an attribute, you will need two arguments. You can set attributes of classes, ids, or whatever you want (you can make attributes called 'title', 'mySpecialAttribute' or whatever you want.
Try this:
$('.external').attr('class', $target);
for further info see the second half of this: http://api.jquery.com/attr/
(where it says 'Description: Set one or more attributes for the set of matched elements.')