Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript jQuery Basics Working with jQuery Collections Stopping the Browser's Default Behavior

code not working,

$pdfBox = $("<label><input type='checkbox'>Allow pdf downloads<label>");


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

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

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


$pdfs.on('click' , function(event){
if($(':checked')).length === 0 {
event.preventDefault();
alert('Allow PDF downloads to proceed.');
}});

$('#links').append($pdfBox);
<!DOCTYPE html>
<html>
<head>
    <title>jQuery Weekly</title>
    <link rel="stylesheet" href="css/style.css" type="text/css" media="screen" title="no title" charset="utf-8">
</head>
<body>
<div class="box">
    <h1>jQuery Weekly</h1>
    <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'>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>
    </div>
</div>
    <script src="js/jquery-3.2.1.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="js/app.js" type="text/javascript" charset="utf-8"></script>
</body> 
</html>

check boxes and 'locked' 'safe' tags also go away. when i remove the "on" click function everything seems to be working

1 Answer

your onclick isn't closed properly

$pdfs.on('click' , function(event){
if($(':checked').length === 0) {
event.preventDefault();
alert('Allow PDF downloads to proceed.');
}});