1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to See/Display the checkboxes that are supposedly dynamically created.

Discussion in 'JavaScript and AJAX' started by vlad2k9, Jan 30, 2009.

  1. vlad2k9

    vlad2k9 New Member

    Jan 29, 2009
    Likes Received:
    Trophy Points:
    Tours guide in Japanese and English
    I have a <textarea> and a few lines in it. I have isolated the lines - as a manner of questions. Now, I want to dynamically generate a set of <checkboxes> where each line will be an option, just like this:

    [ ] lastline
    [ ] one before the last
    [ ] . . .
    [ ] . . .
    [ ] second line
    [ ] first line

    where each pair of brackets is the ACTUAL checkbox. It is like an online test :)

    I found the following portion of code on the web:


    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 = 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"));


    Could you please tell me where in the code above the actual checkboxes are displayed?
    Just for testing purposes, I did put the above code between <script></script> tags, and
    named it "create_them()"; then I called it from the inside of my html <body> from an "onclick" button event in my form. Just like this:
    . . .
    . . .
    <input type="button" onclick="create_them()" value="create boxes"/>
    . . .
    . . .
    I can't understand the workings of that code that I found on the web, all i get is a blank screen.

    Could you please shed some light as of what the workings of the code in question will be? Do I need to add something else?
    I also found some tree generator in JavaScript in this same forum, but seems to be too complicated although neatly coded.

    Your help will be greatly appreciated.

    Thanks beforehand.

  2. skp819

    skp819 New Member

    Dec 8, 2008
    Likes Received:
    Trophy Points:
    you can learn it from the w3schools.com

Share This Page