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 trialVladimir Plokhotniuk
5,464 PointsWhy i copie the code from video 1:1, ind code dont working property.
$pdfs.on('click', function(event) { if ($(':checked').lenght === 0) { event.preventDefault(); alert('Allow dw!'); } });
but this working, not full:
$pdfs.on('click', function(event) {
event.preventDefault();
}
3 Answers
josephr
18,877 PointsYou misspelled length in that first bit of code (($(':checked').lenght === 0)). To be honest, I'm not sure that is the only problem, but it certainly would prevent the code form working.
Daniel Amazigo
Courses Plus Student 11,357 PointsHi! To add to what Joseph explained, I believe JavaScript runs in the order it was written i.e. top to bottom (with some exceptions - like when functions are called) and that's the way the code is loaded onto your browser, I think.
So, the way Joseph has written his code ensures there's actually a checkbox to do a " (':checked').length " for - otherwise the section where the check runs will 'wonder' where the checkbox is, because it hasn't 'seen' the checkbox prior to that moment.
Vladimir Plokhotniuk
5,464 Pointsi deleted " .length " and write again, and that working! But i checked, there wasn't missed spaces or another mistakes, i dont know why that dont worked. Thanks Joseph for help
Joseph Thomas
Courses Plus Student 9,067 PointsI am not sure if anyone answered your question yet, but I will do it now. I also ran into the same problem. My solution was to append the button before the if statement. I have pasted my code below.
const $odd = $('a:odd');
const $secureLinks = $('a[href^="https://"]');
const $pdfs = $('a[href$="pdf"]');
const $pdfCheckbox = $('<label><input type="checkbox"> Allow PDF Downloads </label>');
$secureLinks.attr('target', '_blank');
$pdfs.attr('download', true);
$('#links').append($pdfCheckbox); //Append button here
$secureLinks.addClass('secure');
$pdfs.addClass('pdf');
$pdfs.on('click', function(e){
if($(':checked').length === 0) {
event.preventDefault();
alert('Please check the box to allow PDF downloads.');
}
});