473,403 Members | 2,222 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,403 software developers and data experts.

Dynamically adding a form element and field to a page

Is it possible to dynamically create a new form object (form1), then create
a new form field object and add it form1, and then add form1 to the current
document? I need to do all this in script rather than using the html <form>
and related tags. Can this be done to support both IE, Firefox?

Any code snippets or samples showing how to do this would be fantastic.
Thanks!

--- Mike
Mar 2 '06 #1
5 2062
Mike Dee said the following on 3/2/2006 12:47 AM:
Is it possible to dynamically create a new form object (form1),
Yes.
then create a new form field object and add it form1,
Yes.
and then add form1 to the current document?
Yes.
I need to do all this in script rather than using the html <form>
and related tags. Can this be done to support both IE, Firefox?
Yes.
Any code snippets or samples showing how to do this would be fantastic.


createElement()
appendChild()
so on.
Search the archives.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Mar 2 '06 #2
Thanks for the tips Randy. I was able to write some JS that does this.
However there is one remaining challenge I was hoping you had some thoughts
on.

I now am dynamically creating the form and a form field just fine. However
if I go to another page and then press the back button, the form is
destroyed and form field values are lost. This behavior is different though
than if I hard code a form instead of creating it dynamically.

For instance when I create a form using HTML instead of programmatically via
the DOM and set a form field value in script, if I leave the page and then
return, the browser preserves that form field and its value as it was last
left. This is the same exact behavior I need when I programmatically build
the form, instead of it being destroyed. Is this possible? It would seem
so - why does the browser care how the form was created (either html or
dom) - I wouldn't think that would make a difference but apparently it does.
Thanks!

--- Mike
"Randy Webb" <Hi************@aol.com> wrote in message
news:po********************@comcast.com...
Mike Dee said the following on 3/2/2006 12:47 AM:
Is it possible to dynamically create a new form object (form1),


Yes.
then create a new form field object and add it form1,


Yes.
and then add form1 to the current document?


Yes.
I need to do all this in script rather than using the html <form> and
related tags. Can this be done to support both IE, Firefox?


Yes.
Any code snippets or samples showing how to do this would be fantastic.


createElement()
appendChild()
so on.
Search the archives.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices -
http://www.JavascriptToolbox.com/bestpractices/

Mar 2 '06 #3
Mike Dee said the following on 3/2/2006 9:56 AM:
Thanks for the tips Randy. I was able to write some JS that does this.
Tip #1: Don't top-post here, it is frowned upon like the plague :)
However there is one remaining challenge I was hoping you had some thoughts
on.

I now am dynamically creating the form and a form field just fine. However
if I go to another page and then press the back button, the form is
destroyed and form field values are lost. This behavior is different though
than if I hard code a form instead of creating it dynamically.
Yes. When you hard code it, it is there for the browser and there is
nothing left to chance as long as the HTML is valid code to start with.
When you introduce JS dependency you also introduce unreliability that
you are seeing.
For instance when I create a form using HTML instead of programmatically via
the DOM and set a form field value in script, if I leave the page and then
return, the browser preserves that form field and its value as it was last
left. This is the same exact behavior I need when I programmatically build
the form, instead of it being destroyed. Is this possible?


That depends, directly, on how you are creating the form. If the form
requires user interaction to be created, then no, you aren't going to
*easily* be able to do what you want. You could try setting a cookie but
you would run out of space in a hurry if your form is very large though.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Mar 2 '06 #4

"Randy Webb" <Hi************@aol.com> wrote in message
news:U-********************@comcast.com...
For instance when I create a form using HTML instead of programmatically
via the DOM and set a form field value in script, if I leave the page and
then return, the browser preserves that form field and its value as it
was last left. This is the same exact behavior I need when I
programmatically build the form, instead of it being destroyed. Is this
possible?


That depends, directly, on how you are creating the form. If the form
requires user interaction to be created, then no, you aren't going to
*easily* be able to do what you want. You could try setting a cookie but
you would run out of space in a hurry if your form is very large though.


Thanks for the tips. The form I am creating dynamically is being done in
the document's main OnLoad event, so it does not require user interaction to
be created. However, it still does not persist if you move off the page and
come back with the back button. It will recreate it just fine, but then I
lose the values that I had purposely stored in the form field. Any way to
work around this and make the form persist? Cookies are not an option.

As an alternative to storing this value in a form field - is there any other
nook or cranny within the browser I could tuck a value into that would
persist if a user moved away and then came back with the Back button? Maybe
adding something to a style / stylesheet or sometihng? I would not want to
value to persist if the page is recalled via a link later - just need it to
persist and be present in the even tthe back button is used. I know this
sounds like a cookie could help but as mentioned cookies cannot be used at
all in this solution.

Thanks!

--- Mike
Mar 3 '06 #5
Mike Dee said the following on 3/3/2006 1:10 PM:
"Randy Webb" <Hi************@aol.com> wrote in message
news:U-********************@comcast.com...
For instance when I create a form using HTML instead of programmatically
via the DOM and set a form field value in script, if I leave the page and
then return, the browser preserves that form field and its value as it
was last left. This is the same exact behavior I need when I
programmatically build the form, instead of it being destroyed. Is this
possible? That depends, directly, on how you are creating the form. If the form
requires user interaction to be created, then no, you aren't going to
*easily* be able to do what you want. You could try setting a cookie but
you would run out of space in a hurry if your form is very large though.


Thanks for the tips. The form I am creating dynamically is being done in
the document's main OnLoad event, so it does not require user interaction to
be created. However, it still does not persist if you move off the page and
come back with the back button. It will recreate it just fine, but then I
lose the values that I had purposely stored in the form field. Any way to
work around this and make the form persist? Cookies are not an option.


If you want the form there every time the page is loaded, why not hard
code the form?
What a browser does when you hit the Back button is up to the browser.
As an alternative to storing this value in a form field - is there any other
nook or cranny within the browser I could tuck a value into that would
persist if a user moved away and then came back with the Back button?
Not really, unless you stored it on the server.

Maybe adding something to a style / stylesheet or sometihng? I would not want to
value to persist if the page is recalled via a link later - just need it to
persist and be present in the even tthe back button is used. I know this
sounds like a cookie could help but as mentioned cookies cannot be used at
all in this solution.


If the user leaves to another part of your site? Frames maybe and keep
the data in the parent frame. It would still break if I went from your
site to microsoft.com to google.com and then Back button'ed my way back.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Mar 4 '06 #6

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

Similar topics

10
by: Melissa Mussitsch | last post by:
I've done this before while creating a brand new table. But the code below is not working and I keep getting the error: "Internet Explorer cannot open the Internet site ..... Operation aborted" ...
5
by: Good Man | last post by:
Hi there I'm adding form fields on the fly with some javascript DOM programming. I basically just clone a hidden <div>, then adjust node properties to make this new <div> have unique values...
3
by: Kiyomi | last post by:
Hello, I create a Table1 dynamically at run time, and at the same time, I would like to create LinkButton controls, also dynamically, and insert them into each line in my Table1. I would...
8
by: Donald Xie | last post by:
Hi, I noticed an interesting effect when working with controls that are dynamically loaded. For instance, on a web form with a PlaceHolder control named ImageHolder, I dynamically add an image...
4
by: Stone Chen | last post by:
Hello, I have form that uses javascript createElement to add additional input fields to it. However, my validating script will not process new input fields because it can only find the named...
1
by: adamredwards | last post by:
I have a page with some form elements that are dynamically generated. They are inserted into the dom by first cloning a node, changing the values like name, and then inserted with insertBefore(). ...
7
by: Ron Goral | last post by:
Hello I am new to creating objects in javascript, so please no flames about my coding style. =) I am trying to create an object that will represent a "div" element as a menu. I have written...
1
by: The Eclectic Electric | last post by:
I'd be very grateful if anyone could help me with this. From my limited knowledge of Javascript I don't think it is possible, but I'll punt anyway. I downloaded and very slightly adapted this...
4
by: Lewis Holmes | last post by:
Hi I have the following situation in one of my asp.net pages. The user can add multiple table rows to a form by selecting a button. These rows can contain asp.net controls. When this button is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.