This is my JavaScript function to generate checkboxes at run time based on an XML file when a user clicks on a create button:
function createform(){
var xml = '<root><note ><to>x1</to><from><firstname>x2</firstname><lastname>x3</lastname></from><heading>x4</heading></note><note><to>x5</to><from><firstname>x6</firstname><lastname>x7</lastname></from><heading>x8</heading></note></root>';
var node = (new DOMParser()).parseFromString(xml, "text/xml").documentElement;
var nodes = node.querySelectorAll("*");
for (var i = 0; i < nodes.length; i++) {
var label = document.createElement('label');
var br = document.createElement('br');
var alabel = document.getElementById('div');
var last = alabel[alabel.length - 1];
label.htmlFor = "lbl"+i;
label.appendChild(Createcheckbox(nodes[i].tagName));
label.appendChild(document.createTextNode(nodes[i].tagName));
label.appendChild(br);
document.getElementById('div').appendChild(label);
}
}
function Createcheckbox(chkboxid) {
var checkbox = document.createElement('input');
checkbox.type = "checkbox";
checkbox.id = chkboxid;
checkbox.value = chkboxid;
checkbox.checked = false;
return checkbox;
}
Also on my gsp page, I have this HTML script.
<body>
<g:form name="myForm" controller="dynamicform" action="index">
<input type="button" id="btncreate" value="Create" onclick="createform()"/>
<g:submitButton name="submit" value="Submit" />
<Div id='div'>
</div>
</g:form>
</body>
Since the elements in this form are unknown until the user clicks the "create" button, I'm not sure what is the best way to handle this form.
I need to pass the values of the elements to a controller to render them in a view (I'll need to use these values later to retrieve some data from my database).
I tried different ways to view these elements after submission, such as:
for (name in params.list('name')) {
println name
}
and
params.name.each{i->System.out.println(i);}
It seems that my params are not passed to the controller. Is that because my form elements generated using regular HTML instead of Grails forms <g: checkbox ... >, so the DOM elements can't be passed to the controller? If yes, is there a way to re-write my createform() function using Grails form?
I'm trying to follow best practices while learning Grails so I'd appreciate any suggestion or advice about that.

params.list('name')andparams.nameboth look for parameters named "name" - to see all of your parameter names and values useprintln paramsat the beginning of the controller method. - Burt Beckwithprintln paramsin controller? Within a form, any input/select/checkbox withnameattribute will be passed to controller. In your JS functioncreateform()doesn't seem to have codes for addingnameattribute, can you post theCreatecheckboxtoo? did you check in browser console whats passing with the http request? Did you inspect you page if the generated html codes are in right place (in form)? - devbd