JavaScript jQuery Basics Working with jQuery Collections Changing Element Properties

Is setting a boolean attribute to true valid?

From the video, I see this sets the attribute value to the string 'true' in the DOM. According to the HTML specification

If the attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for the attribute's canonical name, with no leading or trailing whitespace.

The values "true" and "false" are not allowed on boolean attributes. To represent a false value, the attribute has to be omitted altogether.

And XML specifications accept only the full attribute name.

I understand all major browsers will accept attribute='true', and I wouldn't stop anyone from doing it; however, an exercise is trying to make me put .attr('download', true) when the standards indicate otherwise. The download attribute is somewhat odd, since it's not even boolean:

The download attribute, if present, indicates that the author intends the hyperlink to be used for downloading a resource. The attribute may have a value; the value, if any, specifies the default file name that the author recommends for use in labeling the resource in a local file system.

Compliant web-browsers would offer the name 'true' for the file to save. As a separate matter, that exercise may need some attention.

2 Answers

Steven Parker
Steven Parker
176,634 Points

It looks like you've discovered a course bug! You might want to submit this directly to the staff using the procedure described on the Support page.

Thanks!

is there any other way than giving attr.('download' true);

Steven Parker
Steven Parker
176,634 Points

Apparently, it's not a boolean value, so the jQuery way would be ".attr('download', '')".
You can also assign the property with plain JavaScript or just add it to the HTML tag.

According to the spec for download

The attribute may have a value; the value, if any, specifies the default file name that the author recommends for use in labeling the resource in a local file system.

The spec for boolean attributes says

If the attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for the attribute's canonical name, with no leading or trailing whitespace.

So compliant alternatives would be

  • .attr('download', '')
  • .attr('download', filename)
Rob Allessi
STAFF
Rob Allessi
Treehouse Staff

Thanks, Luis! We'll look into it. We're currently on a company retreat, so this investigation will likely be delayed until next week.