Skip to content
This repository was archived by the owner on Dec 20, 2025. It is now read-only.
54 changes: 53 additions & 1 deletion example/js2form.example.html
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,65 @@
<label><input type="radio" name="foo.radio" value="4"> 4</label>
</dd>
</dl>


<table>
<tbody>
<tr>
<th>Phone</th>
<td><input type="text" id="phone" name="phone" /></td>
</tr>
<tr>
<th>Address</th>
<td>
<table id="addressInfo" class="condensed-table">
<tr>
<th>Street 1</th>
<td><input type="text" id="address.street1" name="address.street1" /></td>
</tr>
<tr>
<th>Street 2</th>
<td><input type="text" id="address.street2" name="address.street2" /></td>
</tr>
<tr>
<th>City</th>
<td><input type="text" id="address.city" name="address.city" /></td>
</tr>
<tr>
<th>State</th>
<td><input type="text" id="address.state" name="address.state" /></td>
</tr>
<tr>
<th>Zip</th>
<td><input type="text" id="address.zip" name="address.zip" /></td>
</tr>
<tr>
<th>Country</th>
<td><input type="text" id="address.country" name="address.country" /></td>
</tr>
</table>
</td>
</tr>
</tbody>
</table>

<button type="button" onclick="getJson()">Get JSON</button>
<button type="reset">Reset form</button>

</form>

<div>
<textarea id="src" cols="70" rows="20">
{
"phone":"444-444-4444",
"address":{
"street1": "12345",
"street2": "main st",
"city": "SF",
"state": "CA",
"zip": "94444",
"country": "USA"
},
"foo":{
"radio":"3",
"name":{
Expand Down Expand Up @@ -131,7 +183,7 @@
{
var data = document.getElementById('src').value;
data = JSON.parse(data);
js2form(document.getElementById('testForm'), data);
js2form(document.getElementById('testForm'), data, '.', null, true);
}
</script>
</body>
Expand Down
15 changes: 9 additions & 6 deletions src/form2js.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var form2js = (function()
i = 0;

/* If rootNode is array - combine values */
if (rootNode.constructor == Array || (typeof NodeList != "undefined" && rootNode.constructor == NodeList))
if (rootNode.constructor == Array || (typeof NodeList != 'undefined' && rootNode.constructor == NodeList))
{
while(currNode = rootNode[i++])
{
Expand Down Expand Up @@ -242,8 +242,11 @@ var form2js = (function()
result = [callbackResult];
}
else if (fieldName != '' && node.nodeName.match(/INPUT|TEXTAREA/i)) {
fieldValue = getFieldValue(node);
result = [ { name: fieldName, value: fieldValue} ];
fieldValue = getFieldValue(node);
if (fieldValue == null && node.type == 'radio')
result = [];
else
result = [ { name: fieldName, value: fieldValue} ];
}
else if (fieldName != '' && node.nodeName.match(/SELECT/i)) {
fieldValue = getFieldValue(node);
Expand Down Expand Up @@ -274,8 +277,8 @@ var form2js = (function()
switch (fieldNode.type.toLowerCase()) {
case 'radio':
case 'checkbox':
if (fieldNode.checked && fieldNode.value === "true") return true;
if (!fieldNode.checked && fieldNode.value === "true") return false;
if (fieldNode.checked && fieldNode.value === 'true' || fieldNode.value === 'on') return true;
if (!fieldNode.checked && fieldNode.value === 'true' || fieldNode.value === 'on') return false;
if (fieldNode.checked) return fieldNode.value;
break;

Expand Down Expand Up @@ -312,7 +315,7 @@ var form2js = (function()

if (!multiple) return selectNode.value;

for (options = selectNode.getElementsByTagName("option"), i = 0, l = options.length; i < l; i++)
for (options = selectNode.getElementsByTagName('option'), i = 0, l = options.length; i < l; i++)
{
if (options[i].selected) result.push(options[i].value);
}
Expand Down
10 changes: 7 additions & 3 deletions src/js2form.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,18 @@ var js2form = (function()

if (field instanceof Array)
{
for(i = 0; i < field.length; i++)
for (i = 0; i < field.length; i++)
{
if (field[i].value == value) field[i].checked = true;
if (value == 'on' || value == 'true' || value == '1')
field[i].checked = true;
else
field[i].checked = false
}
}
else if (_inputOrTextareaRegexp.test(field.nodeName))
{
field.value = value;
if (value)
field.value = value;
}
else if (/SELECT/i.test(field.nodeName))
{
Expand Down