Alrighty then, here is the code for the curious, since I cannot leave my test page online. Change MyForm to whatever you called your form.
Code:
<SCRIPT LANGUAGE="JavaScript">
<!-- Author: M. A. Ruberto -->
//This section of code will copy the values in hidden fields and insert them-
//into a set of editable fields. Name the fields what you like but make sure
//each hidden field has a corresponding editable field next to it.
function getWidgets() //aka caveman
{
var editFields = new Array()
var hiddenFields = new Array()
var j = 0
var k = 0
//caveman hungry...
for(formItems=0; formItems<document.forms['MyForm'].length; formItems++){
var widgets = document.forms['MyForm'].elements[formItems];
if(widgets.type == "button"){
continue
}//caveman not like button
if(widgets.type == "submit"){
continue
}//caveman not like submit
if(widgets.type != "hidden"){
editFields[k] = widgets.name
k++
}//caveman eat everything he sees
if(widgets.type == "hidden"){
hiddenFields[j] = widgets.name
j++
}//caveman eat the hiding ones too
//burp
}
//alert(editFields.toString())
//alert(hiddenFields.toString())
populateFields(editFields, hiddenFields)
}//caveman throw up!
function populateFields(editFields, hiddenFields){
var thisForm = document.forms['MyForm'];
//lets use the hidden list as an index for main loop
for(field=0; field<=hiddenFields.length; field++){
for(element=0; element<thisForm.length; element++){
if(thisForm.elements[element].name == hiddenFields[field]){
var hiddenVal = thisForm.elements[element].value
}//each time through main loop pick the hiding ones from cavemans puke
}
for(n=0; n<thisForm.length; n++){
if(thisForm.elements[n].name == editFields[field]){
thisForm.elements[n].value = hiddenVal
}//each time through main loop assign the hidden to the visible
}
}
}
</SCRIPT>
Add the following to your <BODY> after the </FORM>:
Code:
<script type="text/javascript">
<!--
getWidgets();
//-->
</script>
I'm not done adding the ability to handle checkboxes and lists and probably won't get to finish it today. I'll post that revision when I get around to it. Enjoy!
Here is the new code to allow handling of different types of form elements. I got lazy with multiple selects because I don't really use them anyway. I've tested this with a handful of different form pages and so far it all works well. Replace the populateFields() function with the following code.
Code:
function populateFields(editFields, hiddenFields){
var thisForm = document.forms['choiceForm'];
//lets use the hidden list as an index for main loop
for(field=0; field<=hiddenFields.length; field++){
for(element=0; element<thisForm.length; element++){
if(thisForm.elements[element].name == hiddenFields[field]){
var hiddenVal = thisForm.elements[element].value
}//each time through main loop pick the hiding ones from cavemans puke
}
for(n=0; n<thisForm.length; n++){
if(thisForm.elements[n].name == editFields[field]){
var widgetType = thisForm.elements[n].type;
//alert(widgetType)
switch(widgetType){
case "checkbox":
if(hiddenVal == 1){
thisForm.elements[n].click()
}
break
case "select-one":
thisForm.elements[n].selectedIndex = hiddenVal
break
case "select-multiple":
thisForm.elements[n].selectedIndex = hiddenVal
break
case "radio":
if(hiddenVal == 1){
thisForm.elements[n].click()
}
break
default: thisForm.elements[n].value = hiddenVal
}
}//each time through main loop assign the hidden to the visible
}
}
}
Bookmarks