David Mark, my favorite javascript guy!
Hey!
Yes, I am trying to get the user input from checkbox, radio, input,
and
select.
There are inputs, selects and textareas (checkbox and radio are
_types_ of inputs).
It seems the input is retrieved via innerHTML but the other is not.
The results of that proprietary property are implementation
dependant. You will have to traverse the DOM and use the checked,
value and selectedIndex properties to determine the states of the
form
controls. It sounds as if the browsers you tested were referencing
the defaultChecked and defaultSelected properties for radio/checkbox
inputs and select options, but not the defaultValue property for
text
inputs. That's certainly an incongruous approach.
Also, if you must deal with multi-selects, there is no single
property
to reference (you have to loop through the options and check the
selected property for each).
And I've got to ask, what is the purpose of this? Seems like a can
of
worms that doesn't need to be opened.
Please don't top-post. It screws up the context of the dicussion
(and
I don't care to restore it).
Purpose: Document a page into an archive (with user input) via ajax.
These
pages are basically user forms that are then sent to a document
archive
service. Without the user input, archival is useless.
Deja vu. Haven't we had this discussion before?
Personally, I would serialize the data without the markup.
We have and I didn't find an answer. Prior to this, the archival
software
was not finished. The archival software is now complete and working
like
a
champ turning html into beautiful pdf exactly like the html form. So
now
the
project is going live and I need to resolve this last issue.
You will have to traverse the form and build the HTML string yourself
as innerHTML is not an appropriate format to store (varies from one
browser to the next). Have you tried to do that? If you only need
form controls, it is fairly trivial. First define what sort of form
controls will be involved (e.g. what about textareas?)
Start with something like this:-
function serializeFormHtml(name) {
var el = document.forms[name];
var control, controls = el.elements;
var result = [];
for (var i = controls.length; i--
{
control = controls
;
switch (control.tagName.toLowerCase()) {
case 'input':
...
break;
case 'select':
...
break;
case 'textarea':
...
break;
}
}
return result.join('');
}
You will have to fill in the blanks as only you know the context of
your application. For example, other than name and value; what, if
any, attributes should be included? Will there be multi-selects, do
you care about disabled controls, etc.?