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 Changing Element Styles and Classes

Roald Jurrian Kamman
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Roald Jurrian Kamman
Front End Web Development Techdegree Graduate 15,543 Points

.attr, .css, and .addClass not working.

Hey everyone. for some reason these JQuery methods aren't working for me: .attr, .css, and .addClass.

I am still left with a completely unedited page, there is no download present on the pdf and it simply links to the pdf. _blank is not added and no backgroundColor change or class has been added.

const $odd = $('a:odd');
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');
<!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>

2 Answers

When I copy and paste your code and run it in the workspace, it works fine

Roald Jurrian Kamman
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Roald Jurrian Kamman
Front End Web Development Techdegree Graduate 15,543 Points

that's odd None of this is working for me. The past JQuery lessons worked fine but in this series of exercises nothing works.

I got up to the rest of it and still, nothing is working.

const $odd = $('a:odd');
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){
  if $(':checked').length === 0){
    e.preventDefault();
    alert('Please check the box');
  }
});

$('#links').append($pdfCheckbox);

$('a').each(function(index, link){
  const url = $(this).attr('href');
  $(this).parent().append(`(${url})`);
});

What could be causing none of this to work? I run google chrome and have an above-average computer.

oh i'm sorry, i must have forgot to save it before i refreshed. I'll look at it again after dinner.

if $(':checked').length === 0){

should be

if ($(':checked').length === 0){

Roald Jurrian Kamman
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Roald Jurrian Kamman
Front End Web Development Techdegree Graduate 15,543 Points

that for some reason fixed all of it? Don't know why the code before the if function was broken. The if statement came after so the rest should have worked fine without it. But for some reason, everything runs now..

not quite sure why that happens either, you'd expect the code to break at the point where the error occurs, not before it.