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

James Shapiro
James Shapiro
2,222 Points

Correct: $("a.external").attr(...) Too lenient, but still accepted: $(".external").attr(...);

Challenge accepts as a correct answer jQuery which modifies every element with class external when it should only accept as a correct answer jQuery which modifies every list element with class external.

2 Answers

Steven Parker
Steven Parker
229,744 Points

If you examine the HTML part of the challenge, you'll see that only the link elements have the class "external", so just selecting by class does in fact literally satisfy the challenge requirements.

While I would agree that "a.external" is a better choice for the requested conditions, either selection is a valid and correct answer.

James Shapiro
James Shapiro
2,222 Points

From a programming perspective, I beg to differ. This is a case where you get "lucky" by doing the wrong thing and it doesn't cause any immediate problems, but that's not to say that it's correct.

Suppose that someone else adds the external class to another type of element in the future?

Or suppose that you apply the rule to "a" instead of ".external"? I think you would surely agree that that is incorrect, because there's a high likelihood that another developer could add an "a" element without class "external" that they do not want to be modified by the rule. Making a rule overly broad is a mistake, plain and simple, even if nothing breaks immediately.

Steven Parker
Steven Parker
229,744 Points

There's no information given that would make a class-only rule incorrect.

From a programming perspective, the most important aspect of the task is to examine the document and understand how the task requirements relate to it.

Applying the rule to the class external does quite literally satisfy the requirements. The challenge does not explain what the purpose of the class is, and it could be that the class was added to these elements for the specific purpose of associating the CSS rule with them in a compact and reusable way. This is, in fact, a common technique employed by developers.

You'll find that many challenges accept more than one correct solution. I suspect that this one in its current form serves the purpose it is intended for. But if the intention is to require a more specific selector as the answer, I would not expect that the HTML code would be provided at all. Or if it was, I would expect it to contain some additional links that do not have the external class, and some non-link elements with that class.

But as it is, "a.external" is not the only answer that meets the requirements, and if the challenge requirements were to be extended with the phrase "using the most compact selector possible", it would not even be correct at all.