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

Alexandra Velez
seal-mask
.a{fill-rule:evenodd;}techdegree
Alexandra Velez
Front End Web Development Techdegree Student 9,313 Points

Why is Prevent Default Not Working?

https://w.trhou.se/0mzoulpqvm

In this lesson, the idea is that the PDFs should not download because preventDefault is at work in the code.

When I visit the preview of this code, the downloads are happening without an alert. I've checked the code against the instructor's code, and I haven't spotted the error.

Why is prevent default not blocking the downloads?

Here's the lesson https://teamtreehouse.com/library/stopping-the-browsers-default-behavior

1 Answer

Hi Alexandra, there were issues with your quote marks in and also with the code to select https and pdf urls. Here's the correct code:

const $odd = $('a:odd');
const $secureLinks = $('a[href$="http://"]'); // finds urls with https
const $pdfs = $('a[href$=".pdf"]'); // finds all documents ending in .pdf

$secureLinks.attr('target', '_blank');
$pdfs.attr('download', true);

$secureLinks.addClass('secure');
$pdfs.addClass('pdf');

$pdfs.on('click', function(event){
  event.preventDefault();
  // check if checkbox is checked
  // if zero checkboxes are checked
  // prevent download of document
  // alert the user
  // else allow the download
});