Creating checkboxes in JavaScript

Because I always forget how to create checkboxes dynamically in JavaScript so Internet Expoler will be happy:

var i = document.createElement("input");
i.type = "checkbox";
i.className = "CheckBox";
i.id = "uniqueID";
// this is what I forget - Firefox allows .checked, IE must have .defaultChecked
i.defaultChecked = [true/false];
// always use labels
var l = document.createElement("label");
// ALWAYS use for attributes on your labels. FF allows .for, IE must have .htmlFor
l.htmlFor = "uniqueID";
l.appendChild(document.createTextNode("checkbox label"));
This entry was posted in Web Development and tagged , , . Bookmark the permalink.

One Response to Creating checkboxes in JavaScript

  1. Ivan Fernandez says:

    Dear Sherzy,

    How do you actually make the checkboxes appear after the code above described.

    Neat coding!

    Ivan.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>