This is the code I’m using, but I get an error at the first line to appendchild. (unexpected call to method or property access) I’m a newbie to JS and can’t find a simple example of how to correctly build a checkbox list. Where have I gone wrong?
var _div = document.getElementById(“CmtFld”);
var el;
//first remove all existing checkboxes
while(_div.hasChildNodes( ))
{
for(var i = 0; i < _div.childNodes.length; i++)
{
_div.removeChild(_div.firstChild);
}
}
for(var i=0; i < charray.length;i++)
{
el = document.createElement("input");
el.setAttribute("type","checkbox");
el.setAttribute("name","Cmts");
el.setAttribute("value",charray[i]);
_div.appendChild(el);
_div.appendChild(document.createTextNode(charray[i]));
_div.appendChild(document.createElement("br"));
}
Subject: Maybe I wasn’t clear…
I have a form with a text area of multiple entries. I need to prompt the user to select one of the entries to work with. So, I’m opening up a small prompt window that should contain a checkbox list of entries from the text area on the main form.
I’ve got the text area entries in an array, but I can’t get them into a checkbox list so the user can choose one. Actually, now that I think about it, I should be using a radio button list instead. (hope thats not too different)
So, how do I get my array of values to appear as choices?
Subject: How do I populate a checkbox list using Javascript?
How about this …
http://www.dustindiaz.com/check-one-check-all-javascript/
function CheckAll(fmobj) {
for (var i=0;i<fmobj.elements.length;i++) {
var e = fmobj.elements[i];
if ( (e.name != 'allbox') && (e.type=='checkbox') && (!e.disabled) ) {
e.checked = fmobj.allbox.checked;
}
}
}