Having a checkbox in an xform checked by default

Vote:
 
Hi, When creating an xform, I have several checkboxes, and I would like some of them to be checked by default. Does anybody know how to do this? If not in the editor, is there a way to achieve this programmatically?
#12660
Mar 25, 2008 18:35
Vote:
 
Actually I think it should be possible to speficy default values for ALL the different form fields in an XForm, not only the checkboxes. However, it does not seem possible in the editor. Am I missing somehing? I am using 4.51..
#14698
Mar 25, 2008 18:45
Vote:
 
OK, a workaround is in place! :-) Since we are dealing with a list of checkboxes, and the ID is specified for the list as a whole, I figured I couldn't really use the name or the cssclass fields to make some sort of work-around. I needed to be able to check only one specific checkbox of the ones available, and each checkbox is defined by specifying alternatives in the editor. So I figured I had to work with these. The solution I came up with was this: Tell the editor that if the checkbox should be checked by default, use the suffix "Checked" for the alternative. For example, if there are three checkboxes defined by the alternatives "phone", "email" and "post", and "email" should be checked by default, it should be specified as "emailChecked". Then I produced a recursive javascript that iterates through all the controls on the xform and looks for controls of type checkbox with a value that ends with "Checked". If one is found, it is checked, and the string "Checked" is removed from the corresponding label. The recursive javascript function looks like this: function CheckSomeCheckboxes(element) { try { if (element.type) { // look for checkbox if ((element.tagName.toLowerCase() == "input") && (element.type == "checkbox") && (element.disabled == false)) { // check if name ends with Checked if (element.value.search(/Checked$/i) != -1) { // check the checkbox element.checked = true; // find the checkbox' sibling (checkbox text is kept in a label next to the checkbox) if ((element.nextSibling) && (element.nextSibling.tagName.toLowerCase() == "label")) { // Remove the "Checked" text from the label element.nextSibling.innerHTML = element.nextSibling.innerHTML.replace(/Checked$/i,""); } } } } } catch(E){alert("hmm:"+E);} if (element.childNodes && element.childNodes.length > 0) { for (var x = 0; x < element.childNodes.length; x++) { CheckSomeCheckboxes(element.childNodes[x]); } } } Below the code which includes the xform (lets say it is contained in a div with id=ContentArea), I then call the function to check the relevant checkboxes: CheckSomeCheckboxes(document.getElementById("ContentArea")); I guess a similar approach can be used to select a default radiobutton etc. Hope this can help others with a similar problem! If anyone have a more elegant solution, please let me know :-) -b
#14699
Mar 25, 2008 18:45
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.