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 jQuery Basics Working with jQuery Collections Stopping the Browser's Default Behavior

yoav green
yoav green
8,611 Points

code down not work

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

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

//$odd.css('backgroundColor', 'lightgrey');

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

$pdfs.on('click', function(e){
  //check if the checkbox has been checked
  //if zero checkboxes are checked
  if ($(':checked').length === 0) {
      //prevent download of document
      e.preventDefault();
      //alert the user
      alert('please check the box to allow pdf download'); 
    }
});

const $checkBox = $('<label><input type='checkbox'> Allow PDF downloads</label>');
$('#links').append($checkBox);
<!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>
<!--        <label><input type='checkbox'> Allow PDF downloads</label>-->
    </div>
</div>
    <script src="js/jquery-3.2.1.min.js"></script>
    <script src="js/app.js"></script>
</body>
</html>

3 Answers

Corey Montgomery
Corey Montgomery
18,468 Points

I can see you have a typo in your JS code. The problem is with your quotes. You need to use double quotes in the string around "checkbox" when you are wrapping it with single quotes.

The following:

const $checkBox = $('<label><input type='checkbox'> Allow PDF downloads</label>');

Should be:

const $checkBox = $('<label><input type="checkbox"> Allow PDF downloads</label>');

This should get you started and at least beyond the error.

Karen Shea
Karen Shea
13,851 Points

I had the same problem. Didn't notice that I had forgotten to change the quotes. Thank you so much for pointing it out!

Corey Montgomery
Corey Montgomery
18,468 Points

In general you are correct. For most strings you can use single or double quotations interchangeably.

const $variable = "This is some text";
const $another = 'This is some text';

However, this does not work when you have content that requires escaping. This is common with strings that have apostrophes in them or as in your case, quotations in the HTML. Hopefully that gives you some better context to the problem. If not, I found this article that describes it much better than I can.

https://www.digitalocean.com/community/tutorials/how-to-work-with-strings-in-javascript

yoav green
yoav green
8,611 Points

thanks bro. what's the difference between the quotes? thought they do the exact same job.