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 Working with jQuery Collections jQuery-Specific Selectors

Ryan Doran
Ryan Doran
7,815 Points

White Space in $('a[href$ = "pdf"]') throws an error

Interesting:

This will hide the pdf files:

const $odd = $('a:odd');
const $secureLinks = $('a[href^="https://"]');
const $pdfs = $('a[href$="pdf"]');
$pdfs.hide(); 

but this will not:

const $odd = $('a:odd');
const $secureLinks = $('a[href^="https://"]');
const $pdfs = $('a[href$ = "pdf"]');
$pdfs.hide();

Apparently, the white space between the href$, the equal sign (=), and then the quotes throws an error:

JavaScript $('a[href$ = "pdf"]')

  Error: Syntax error, unrecognized expression: a[href$ = "pdf"]

1 Answer

Steven Parker
Steven Parker
229,744 Points

White space in general won't cause a problem, but here it was separating the characters of a single operator. The "match at the end" operator is "$=" and can only be recognized by the system when both characters are adjacent. And the name of the attribute being tested is "href", not "href$".

I would expect to see the following behavior:

const $pdfs = $('a[href$ = "pdf"]');  // this would cause a syntax error
const $pdfs = $('a[href $= "pdf"]');  // but this should work
Ryan Doran
Ryan Doran
7,815 Points

Awesome info, that clears up a great deal, thanks Steven.