dynamically create radio buttons

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • obsidian8@hotmail.com

    #1

    dynamically create radio buttons

    Hi All,

    I have looked around for an answer to this question, but haven't found
    one as of yet. I'm trying to use javascript to dynamically create
    raido buttons. I am able to create them easily enough but they aren't
    clickable. It doesn't matter if I set them as checked or not, you can't
    manipulate them at all. The code I'm using appears below:

    function test(){
    componentMainTa ble = document.getEle mentById("mycom ponent");
    componentTable = document.create Element("table" );
    componentBody = document.create Element("tbody" );
    componentMainTa ble.setAttribut e("width","100" );

    //row and table information is created here
    row1 = document.create Element("tr");
    tab1 = document.create Element("td");
    tab1.setAttribu te("width", "33%");
    tab1.setAttribu te("align", "center");
    field1 = document.create Element("input" );
    field1.setAttri bute("type","ra dio");
    field1.setAttri bute("name","on e");
    field1.setAttri bute("value", 1);
    tab2 = document.create Element("td");
    tab2.setAttribu te("width", "33%");
    tab2.setAttribu te("align", "center");
    field2 = document.create Element("input" );
    field2.setAttri bute("type","ra dio");
    field2.setAttri bute("name","on e");
    field2.setAttri bute("value",2) ;
    tab1.appendChil d(field1);
    tab2.appendChil d(field2);
    row1.appendChil d(tab1);
    row1.appendChil d(tab2);
    componentBody.a ppendChild(row1 );
    componentTable. appendChild(com ponentBody);
    componentMainTa ble.appendChild (componentTable );
    field1.setAttri bute("checked", "checked");

    }

    If anyone can shed some light on this for me, I would be tremendously
    grateful!

  • RobG

    #2
    Re: dynamically create radio buttons

    obsidian8@hotma il.com wrote:[color=blue]
    > Hi All,
    >
    > I have looked around for an answer to this question, but haven't found
    > one as of yet. I'm trying to use javascript to dynamically create
    > raido buttons. I am able to create them easily enough but they aren't
    > clickable. It doesn't matter if I set them as checked or not, you can't
    > manipulate them at all. The code I'm using appears below:[/color]

    Your code works provided the HTML includes something like:

    <div id="mycomponent " style="border: thin solid blue">
    </div>

    I have added a border so you can see the elements in the page, remove
    if you wish.
    [color=blue]
    > function test(){
    > componentMainTa ble = document.getEle mentById("mycom ponent");
    > componentTable = document.create Element("table" );
    > componentBody = document.create Element("tbody" );
    > componentMainTa ble.setAttribut e("width","100" );[/color]

    Setting style attributes is better done directly using the style
    object:

    componentMainTa ble.style.width = "100";
    [color=blue]
    >
    > //row and table information is created here
    > row1 = document.create Element("tr");
    > tab1 = document.create Element("td");
    > tab1.setAttribu te("width", "33%");
    > tab1.setAttribu te("align", "center");[/color]

    The same here:

    tab1.style.widt h = "33%";
    tab1.style.text Align = "center";
    [color=blue]
    > field1 = document.create Element("input" );
    > field1.setAttri bute("type","ra dio");
    > field1.setAttri bute("name","on e");
    > field1.setAttri bute("value", 1);[/color]

    Element attributes can be set directly:

    field1.type = "radio";
    field1.name = "one";
    field1.value = "1";

    [...][color=blue]
    > field1.setAttri bute("checked", "checked");[/color]

    field1.checked = true;

    However, it works fine in Firefox but doesn't work in IE. Your
    radio buttons should be in a form, but that doesn't fix anything.
    Below is a minimal implementation of your code...

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head>
    <title>demo</title>
    <script type="text/javascript">

    function test(){

    theSpot = document.getEle mentById("mycom ponent");

    field1 = document.create Element("input" );
    field1.type = "radio";
    field1.name = "one";
    field1.value = "1";

    field2 = document.create Element("input" );
    field2.type = "radio";
    field2.name = "one";
    field2.value = "2";

    theSub = document.create Element("input" );
    theSub.type = "submit";

    theForm = document.create Element("form") ;
    theForm.action = "";
    theForm.appendC hild(field1);
    theForm.appendC hild(field2);
    theForm.appendC hild(theSub);
    theSpot.appendC hild(theForm);
    field1.checked = true;
    }
    </script>
    </head>
    <body onload="test(); ">
    <div id="mycomponent " style="border: thin solid blue">
    </div>
    </body>
    </html>



    --
    Rob

    Comment

    • RobG

      #3
      Re: dynamically create radio buttons

      obsidian8@hotma il.com wrote:[color=blue]
      > Hi All,
      >
      > I have looked around for an answer to this question, but haven't found
      > one as of yet. I'm trying to use javascript to dynamically create
      > raido buttons. I am able to create them easily enough but they aren't
      > clickable. It doesn't matter if I set them as checked or not, you can't
      > manipulate them at all. The code I'm using appears below:[/color]

      Unfortunately, it seems DOM methods don't work with IE here. The
      following innerHTML works in Firefox and IE:


      theSpot = document.getEle mentById("mycom ponent");

      var s = [
      '<form action="">',
      '<input type="radio" name="one" value="1" checked>',
      '<input type="radio" name="one" value="2">',
      '<input type="submit">' ,
      '</form>'
      ];

      theSpot.innerHT ML = s.join('');


      If you want to use innerHTML and a table, you can either fill in the
      content of a cell, or do the entire table. You can't add/remove
      rows/cells with innerHTML (it actually seems to work in Firefox, but
      not in IE - the MS spec says it doesn't work in IE and they're right).



      --
      Rob

      Comment

      Working...