Connecting Tech Pros Worldwide Forums | Help | Site Map

Dynamic radiobuttons with labels in IE

ronnil's Avatar
Expert
 
Join Date: Jun 2007
Posts: 132
#1: Jun 21 '07
Just found an annoying thing in IE

I'm working on this neat little form framework giving me easy-coded forms with some automatic made fields, for instance a boolean field.

My original idea was to make this with radiobuttons, and put them in a nice little table for the pretty looks
Expand|Select|Wrap|Line Numbers
  1. function Boolean()
  2. {
  3.    this.container = document.createElement('table');
  4.    var row = this.container.insertRow(this.container.rows.length);
  5.    var trueCell = row.insertCell(row.cells.length);
  6.    var falseCell = row.insertCell(row.cels.length);
  7.  
  8.    trueOpt = document.createElement('input');
  9.    trueOpt.type = 'radio';
  10.  
  11.    falseOpt = document.createElement('input');
  12.    falseOpt.type = 'radio';
  13.  
  14.    //set values and a bit other here
  15.  
  16.   /*
  17.   now, DHTML doesn't allow dynamic makings of <label> so instead i figured out to implement it like this
  18.   */
  19.  
  20.     trueLabel = '<label for='" + trueOpt.id + '">True</label>';
  21.     trueCell.appendChild(trueOpt);
  22.     trueCell.innerHTML = trueCell.innerHTML + trueLabel;
  23.  
  24.     //repeat with falsecell
  25. }
This ofc works great in FF, but by altering the innerHTML of the trueCell, IE suddenly won't treat the input as a radio option. event's don't fire or anything

I tried with putting
Expand|Select|Wrap|Line Numbers
  1. if(trueOpt.attachEvent)
  2.    trueOpt.attachEvent('onfocus',function(){trueOpt.checked=true});
  3.  
both before and after the innerHTML statement, but without luck.

Easy fix is to use a selectbox with two different values ;D but I pretty much want to implement radiooptions with labels later on...

guess you could make a label like this:
Expand|Select|Wrap|Line Numbers
  1. function Label(id,value)
  2. {
  3.    document.write('<label id="'+id'">'+value+'</label>');
  4.    return document.getElementById(id);
  5. }
  6.  
afterwards put this in trueCell with appendChild....

only problem is, this only works when the document is actually loaded i guess....

what do you guys think?

ronnil's Avatar
Expert
 
Join Date: Jun 2007
Posts: 132
#2: Jun 21 '07

re: Dynamic radiobuttons with labels in IE


heh... just struck me...

put the button and the label in two different cells... well duh! :)
Reply


Similar JavaScript / Ajax / DHTML bytes