Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the proper way to declare an attribute to be false?

In HTML5, you can add the attribute draggable to any element -- it works like checked; it doesn't require a value. If you do want to specify a value (e.g. to make it valid XML), I think the official standard syntax is checked="checked", but other things work too (including checked="" and checked="false", confusingly). The standard way to make it ‘false’ is not to include the attribute at all.

<img> elements seem to be ‘draggable’ by default (even if you don't include a draggable attribute) ­– that's why you get the 'ghost' image effect when you drag regular images in web pages. You get this same effect on non-img elements if you mark them draggable.

So how do you disable it on img elements? You can set the property to false in JavaScript. But what about in the HTML?

(From experimenting, I know that draggable="false" seems to work in Chrome ­– is that a standard syntax that works everywhere? If so, why doesn't checked="false" work the same way?)

like image 670
callum Avatar asked Jul 15 '11 20:07

callum


People also ask

How do you set a boolean attribute?

To set the value of a Boolean attribute, such as disabled , you can specify any value. An empty string or the name of the attribute are recommended values. All that matters is that if the attribute is present at all, regardless of its actual value, its value is considered to be true .

How do you declare an attribute in HTML?

If you want to define your own custom attributes in HTML, you can implement them through data-* format. * can be replaced by any of your names to specify specific data to an element and target it in CSS, JavaScript, or jQuery.

What are the boolean attributes?

A Boolean attribute is an attribute that can only be true or false. How does a Boolean attribute work? According to the HTML specification: The presence of a boolean attribute on an element represents the “true” value, and the absence of the attribute represents the “false” value.

How do you put a boolean in HTML?

To add a Boolean attribute: node. setAttribute(attributeName, ''); // example: document. body.


1 Answers

Well, W3C says you're right; "true", "false" and "auto" should work (not to say it will everywhere).

Edit: http://jsfiddle.net/nwellcome/DRSbc/ for to experiment with in different browsers.

like image 136
nwellcome Avatar answered Nov 10 '22 01:11

nwellcome