473,387 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Creating Form & Element Objects

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?

TIA,

- skubik.
Jul 23 '05 #1
2 2103


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/
Jul 23 '05 #2
"skubik" <vk***@yahoo.com> skrev i meddelandet
news:cCuyd.564723$nl.93802@pd7tw3no...
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?


You'll be able to do this with DOM functions - createElement(),
setAttribute(), appendChild() and so on. Your code will look something like
this:

var theForm = document.createElement("form");
theForm.action = "...";
theForm.method = "...";

var theElement = document.createElement("input");
theElement.type = "button";
....
theForm.appendChild(theElement);
....
var nodeSomewhereInDocument = .....; // using, for instance,
document.getElementById()
nodeSomewhereInDocument.appendChild(theForm);

Joakim Braun
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: gonzalo briceno | last post by:
I have been using phplib for a while and I really like the framework except for form creation. Maybe it is me but I in my opinion there isn't a good way to create forms or should I say, everything...
11
by: Mr. Smith | last post by:
Hello all, My code can successfully open, write to, format and save several worksheets in a workbook then save it by a given name, close and quit excel. My problem is that if I try and do it...
16
by: TTroy | last post by:
I FOUND MAJOR ERRORS in K&R2 (making it almost useless for the herein mentioned topics). K&R2 Section 5.9 Pointers vs. Multidimension Arrays starts of like this... "Newcomers to C are...
1
by: Thomas D. | last post by:
Hello all, I'm using the IXmlSerializable interface for a project and encounter some problems when testing my webservice in a client application. I know this interface is undocumented and not...
1
by: Mike Logan | last post by:
I have a schema that defines my messages and objects. I then have a WSDL that defines the web services. I have my sample XSD, sample WSDL, and the code generated from WSDL.exe. In the generated...
2
by: misower | last post by:
var c = document.getElementById("PanelTree"); // PanelTree is a <div> element! var n = document.createElement("div"); n.setAttribute('id', 'nu'); n.setAttribute('style',...
2
by: Ralph | last post by:
Hi I don't understand why it's not working: function schedule(imTop){ this.tdImagesTop = imTop; } schedule.prototype.selectEl = function() { alert(this.tdImagesTop);
32
by: Joe | last post by:
I am just starting to use Object Oriented PHP coding, and I am seeing quite often the following (this example taken from a wiki): $wakka =& new Wakka($wakkaConfig); What exactly is the =&, and...
9
by: dhtml | last post by:
I have written an article "Unsafe Names for HTML Form Controls". <URL: http://jibbering.com/faq/names/ > I would appreciate any reviews, technical or otherwise. Garrett --...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.