Boolean HTML Attributes

There are some attributes in HTML which are "boolean" - browsers treat them as "true" if they are present, regardless of the value. An example of such an attribute is selected on the <option> tag. Another is checked on <input type="checkbox">.

If you have a call to setAttribute() for such an attribute, there seems to be no value you can set to have the browsers consistently behave as though the attribute is missing.

For example

option.setAttribute("selected", false)

will still mark the option selected. null, empty string or undefined don't work either. If anyone knows of a value I can set to achieve my goal, please let me know, but I don't think one exists. (Because of some framework code I use, not calling setAttribute(), or calling removeAttribute() is difficult.)

I'm trying to find an exhaustive list of such attributes to special case them. Here's what I have so far:

  • selected of <option>
  • checked of <input>
  • disabled, readonly of <input>, <select>, <option>, <optgroup>, <button>, <textarea>
  • multiple of <select>

Please help me complete this list - or point me to an existing one.

-------------Problems Reply------------

(Because of some framework code I use, not calling setAttribute(), or calling removeAttribute() is difficult.)

Then I would submit that the framework code needs fixing, or dumping.

You can't setAttribute to unset an attribute, by design. Any solution you found involving out-of-band values like ‘null’, if it happened to work in any particular browser, would be quite invalid according to the DOM Core standard.

setAttribute() is in any case best avoided in browser (non-XML) HTML contexts. IE pre-8 doesn't know the difference between a DOM attribute and a JavaScript property, which can easily result in many really weird problems. If you're trying to set ‘checked’ as an attribute (which theoretically you should do by setting it to the string "checked"), don't expect IE to co-operate.

The full list of boolean attributes in HTML 4.01 (and hence XHTML 1.0) is (with property names where they differ in case):

checked (input type=checkbox/radio)
selected (option)
disabled (input, textarea, button, select, option, optgroup)
readonly (input type=text/password, textarea)
multiple (select)
ismap isMap (img, input type=image)

defer (script)
declare (object; never used)
noresize noResize (frame)
nowrap noWrap (td, th; deprecated)
noshade noShade (hr; deprecated)
compact (ul, ol, dl, menu, dir; deprecated)

Try removeAttribute:


EDIT: After reading your comment, read this about setAttribute. Notably:

Even though getAttribute() returns null for missing attributes, you should use removeAttribute() instead of elt.setAttribute(attr, null) to remove the attribute.

on table cells e.g. TD, TH


for the record, to change attributes like checked (on checkbox/radio elements) you can do.

myCheckBoxElem.checked = true|false;


myCheckBoxElem.checked = !myCheckBoxElem.checked;//toggles to the opposite state

Can't you just use removeAttribute()?

Not exactly what you're asking about, but both the 'class' and 'for' attributes receive different DOM names


Category:javascript Views:0 Time:2009-04-01

Related post

  • Boolean model attributes as automatic methods 2010-07-13

    I'm working through (currently working on section 10.4.2 - destroying users). My User model has a boolean admin attribute, and in the Users controller there is some simple code making use of this attribute: def admin_user red

  • Ruby on Rails content_tag options hash - setting a boolean html attribute? 2011-03-10

    Boolean attributes, as defined in the html 5 draft specification: The presence of a boolean attribute on an element represents the true value, and the absence of the attribute represents t

  • Correctly format boolean tag attributes with tinymce editor plugin 2012-04-16

    i am building a plugin for tinymce editor which adds some microdata to selected text, and i want to make sure the final markup will be valid. as specified by the draft microdata spec, a new item is indicated by adding the attribute itemscope to an el

  • jQuery select by boolean data attribute 2012-03-12

    I've got this line of code: $(selector).find(":input[data-val=true]") This will correctly select elements like this <input type='number' data-val='true' /> But this will NOT select elements with alternate casing, like so: (note the capital T)

  • XML schema contruct for a type with both enumerations and attributes 2008-12-05

    I'm trying to create a type in an XML schema to enforce an element with both: A single attribute; and Simple content matching an enumeration. In an XML document, the element might look like: <Operator Permutation="true"> Equals </Operator

  • Configure hibernate (using JPA) to store Y/N for type Boolean instead of 0/1 2009-07-20

    Can I setup JPA/hibernate to persist Boolean types as Y/N? In the database (the column is defined as varchar2(1). It currently stores them as 0/1. The database is Oracle. --------------Solutions------------- The only way I've figured out how to do th

  • The Attribute of "ref" for XML Schema For C# Parse 2009-12-10

    Good day. I got a problem about the attribute of "ref" for my XSD file. My code : using System; using System.Collections; using System.Xml; using System.Xml.Schema; class XmlSchemaTraverseExample { static void Main() { // Add the customer schema to a

  • extending spring form tag library attributes 2010-03-26

    I'm using Spring's form tag library in a Spring MVC application that I am developing. The company I am working for has implemented some company-wide policies based on the definition of custom attributes for certain tags. For instance, by default (tho

  • XML Schema for SimpleType and Attribute? 2010-12-21

    I'm trying to create an XML schema that can capture XML that looks something like this: <tagname description="simple string type attribute"> false <!-- simple boolean type --> </tagname> But am running into difficulties. Is it even

  • How can I cross-browser detect input fields with the 'required' attribute in jQuery? 2011-02-12

    I am running into a slight validation issue with the Boolean required attribute on form fields. I am marking up my fields as such: <label for="email">Email Address:</label> <input value="" type="email" name="email" id="email" required

  • Can we write XSD schema for complex element that contain other element and attributes and text (all of them)? 2011-06-16

    I have an XML element which contains other elements, attributes as well as text too. A sample XML data is here: <property readonly="0" eraseable="1" > <property readonly="0" eraseable="1" > <property readonly="0" eraseable="1" >wind

  • Set disable attribute based on a condition for Html.TextBoxFor 2011-07-12

    I want to set disable attribute based on a condition for Html.TextBoxFor in MVC like below @Html.TextBoxFor(model => model.ExpireDate, new { style = "width: 70px;", maxlength = "10", id = "expire-date" disabled = (Model.ExpireDate == null

  • Why won't AR allow me to make an attribute setter private using `private :accepted=`? 2011-08-02

    I've just setup a new boolean accepted attribute on my model that looks like this: class Invitation < ActiveRecord::Base attr_protected :accepted ... end I want it to be a private attribute however when I attempt to remove the public setter like t

  • Rails object attributes / db migration defaults 2012-05-03

    Suppose I have a User class in rails with a boolean is_subscriber attribute. My database migration file is structured like this: class AddIsSubscriberToUsers < ActiveRecord::Migration def change add_column :users, :is_subscriber, :boolean, :defaul

  • How to implement security in a GUI application? 2009-01-21

    I'm writing a GUI application that will have a user log in feature. Each user will belong to (at least one, possibly more than one) group and each group will have attributes indicating if certain rights are allowed or not allowed. The list of rights

  • Database Design Related 2009-06-23

    I would like the users be able to create their accounts (which stores the login and password information - Accounts table) and once the email is verified, they have the option of creating their own person record (which stores the other information su

  • Acts_as_paranoid, is_paranoid- Alternatives? 2009-07-09

    I'm looking for a rails plugin/gem which brings the functionality of marking an ActiveRecord-Model deleted, instead of deleteing it. Does anybody know, what gems or plugins are up to date? (AAP is out-dated and is_paranoid doesn't appear to be used b

  • Using Java Domain Objects With Spring Security Plugin 2009-07-29

    I'm working with a legacy database and corresponding Java Domain Classes. I'm trying to add Spring Security to the app and I'd like to use Java Domain Classes for that too, just to be consistent and keep everything together. Grails has no problem usi

  • Toggle dropdownlist enable and disable with a button click using Javascript? 2009-12-31

    I have the following JavaScript to disable a dropdownlist in a ASP.NET page, which gets called when I click a button. function disableDropDown(DropDownID) { document.getElementById(DropDownID).disabled = true; return false; } I wanted to use the same

Copyright (C), All Rights Reserved.

processed in 0.108 (s). 11 q(s)