Carlos Lantigua5,937 Points
JQuery 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);
Jennifer NordellTreehouse Staff
Hi 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!
Luis Ortiz Hinojosa4,482 Points
Hey Carlos, did you ever get it to work? I also wasn't able to download the file
Alan McClenaghanFront End Web Development Techdegree Graduate 45,524 Points
This 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.
James March7,921 Points
Changing preferences worked for me too, nothing wrong with the code thankfully
Chris KomaroffPro Student 13,666 Points
All 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.