skubik wrote:
I'm curious as to whether it's possible to create a Form object and
populate it with form element objects, strictly in Javascript, without
the need to apply the form to a document.
Essentially, I want to do this:
tmpFormObj = new Form();
tmpFormObj.target = document.location; // For example.
tmpFormObj.method = "POST";
tmpElementObj = new Input();
tmpElementObj.type = "text";
tmpElementObj.value = "HELLO WORLD";
... but I'm not sure whether it's possible to insert the Element object
into the Form object. Furthermore, my Javascript seems to terminate at
the 'new Form()' statement. Is this even possible?
With modern browsers like IE 5/6, Netscape 6/7, Mozilla, Firefox, Opera
7 you can create any HTML element dynamically, you use the W3C DOM to do
that, there are no constructors (e.g Form, Input) as you have tried
above but there is a factory method
document.createElement
that takes the tag name as a string argument so you can do
var form, input;
if (document.createElement && (form = document.createElement('form'))) {
form.action = 'whatever.php';
form.method = 'POST';
input = document.createElement('input');
input.type = 'text';
input.name = 'inputName';
input.value = input.defaultValue = 'Kibology';
However that just creates those elements in memory, you need then to
insert one into the other and also insert the form into the document
form.appendChild(input);
document.body.appendChild(form);
}
Theoretically you could just keep the form object in memory but I doubt
that (all) browsers let you then submit such a form.
If all you want to do is post data to the server then a considerable
amount of browsers allows that using the XMLHTTP request object, see
<http://www.faqts.com/knowledge_base/view.phtml/aid/17226/fid/616>
and a link in the FAQ
<http://jibbering.com/faq/>
--
Martin Honnen
http://JavaScript.FAQTs.com/