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 trialCarlos Lantigua
5,938 PointsJQuery Attr method for download not working : (
Trying to turn the pdf file for JQuery Cheatsheet into a download and it does not seem to be working, it still just opens the pdf in a separate tab.
<div id="links">
<ul>
<li><a href='https://learn.jquery.com/'>Learn jQuery</a></li>
<li><a href='http://treehouse-project-downloads.s3.amazonaws.com/jquery-basics/jquery_cheatsheet.pdf' download>jQuery Cheatsheet</a></li>
<li><a href='https://api.jquery.com/'>jQuery Documentation</a></li>
<li><a href='https://developer.mozilla.org/en-US/docs/Glossary/jQuery'>jQuery Glossary</a></li>
</ul>
<label><input type='checkbox'> Allow PDF downloads</label>
</div>
```JQuery
const $odd = $('a:odd'); //selects all odd anchor elements
const $secureLinks = $('a[href^="https://"]');
const $pdfs = $('a[href$=".pdf"]');
$secureLinks.attr('target', '_blank');
$pdfs.attr('download', true);
6 Answers
Alan McClenaghan
Full Stack JavaScript Techdegree Graduate 56,501 PointsThis seems to be more of a browser setting issue: Follow these steps for Chrome on a Mac:
- Open Google Chrome.
- Click on the Menu icon (Google Chrome Menu icon) in the top-right corner of the Window.
- Click Settings.
- Scroll down to the bottom of the Settings window and click Advanced.
- In the Privacy and security section, click Content Settings.
- Scroll down and click the PDF documents option.
Check out https://www.computerhope.com/issues/ch001114.htm for preferences in other browsers.
Luis Ortiz Hinojosa
4,482 PointsHey Carlos, did you ever get it to work? I also wasn't able to download the file
Carlos Lantigua
5,938 PointsNo I was not, had to move on. Sorry : (
shashi7
11,034 PointsI can't download either...phew!!
James March
7,921 PointsChanging preferences worked for me too, nothing wrong with the code thankfully
Jennifer Nordell
Treehouse TeacherHi there, Carlos! This is tricky to answer because I'm not sure if that final backtick in your code was part of your markdown or not. If it's actually part of your code, then it will cause an error. If it's part of your markdown and not in your code, then the problem is elsewhere. I took your code and pasted it into the workspace and other than that one backtick, it works for me.
So here are a few things to check:
- Make sure you've saved your work. We all forget sometimes.
- Try refreshing the page
- Make sure you've checked "Allow PDF downloads"
- If none of this is working, you might try deleting your browser cache because it could be loading in older data instead of your new code.
Hope this helps, but let me know if there are still issues!
Carlos Lantigua
5,938 PointsThank you for the reply, the back tick is an attempt on my part to quote the code lol. The link is still directing me to view the pdf rather than download the file. At this point in the video it should allow the user to download on click without checking the box as that feature is supposed to be added in the next video. I have tried chrome and firefox, I'm probably misspelling something some where and just can't see it.
Alan McClenaghan
Full Stack JavaScript Techdegree Graduate 56,501 PointsThe "Allow PDF downloads" functionality is added in a later video.
Changing the browser preferences worked for me.
Chris Komaroff
Courses Plus Student 14,198 PointsAll I am able to do for Chrome is drill down to settings | advanced | privacy and security | content settings | Download PDF files instead of automatically opening them in Chrome = Yes / turn on. But of course this is silly because all PDFs download automatically anyway such that I don't need to use the download attribute in the first place. I don't think the anchor tag download attribute is ready for prime time. It sounded like a weird attribute to me when Treasure used it as an example.
Ryan Pierson
14,365 PointsRyan Pierson
14,365 PointsThanks Alan!
Richard Eldridge
8,229 PointsRichard Eldridge
8,229 PointsAlan's method worked for me.
Charles Tillmon
33,364 PointsCharles Tillmon
33,364 PointsThanks Alan! I was scratching my head on this one
Ariful Hoque
3,700 PointsAriful Hoque
3,700 Pointsthanks Alan for the help. This saved me some time.
Emilia Jacaszek
3,003 PointsEmilia Jacaszek
3,003 PointsThank you, download works now !
rhupp
11,019 Pointsrhupp
11,019 PointsBrilliant!
Tomasz Grodzki
8,130 PointsTomasz Grodzki
8,130 PointsIt looks a little bit different for Windows: https://support.google.com/chrome/answer/6213030?hl=en