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 Introduction to jQuery Hello, jQuery! Accessing and Modifying Attributes

Learning coding
seal-mask
.a{fill-rule:evenodd;}techdegree
Learning coding
Front End Web Development Techdegree Student 9,937 Points

What does this part of the code mean?

The teacher types in this piece of code.

$('a').not('[href*="my-domain.com"]').attr("target", "_blank");

This code knows that a site should be started in a new tab. But how does it know that it has to be google? I see no target class in html for instance.

And .not is to remove something. So this part of the code not('[href*="my-domain.com"]' means that the inititial domain get's removed so google can be opened in a new tab?

Can someone expand a bit on how this code is working?

1 Answer

Steven Parker
Steven Parker
229,783 Points

This code is setting the target attribute to the value "_blank" which will cause the link to create a new window.

But you're right, nothing in this code is specific to google, that's determined by the href attribute which is set in the HTML code. And that attribute is not being changed here.

The .not() method is not changing the document, but only removing links from the selected list which reference our own site (my-domain.com").

So if we break it down, what the code does overall is to

  • $('a') :point_left: change all links
  • .not('[href*="my-domain.com"]') :point_left: which are not to our own site
  • .attr("target", "_blank") :point_left: so that they will open in a new window

Does that make it clearer now?