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

Dynamically move elements into form ?

I was asked to do this....
Dynamically move "text1" and "hidden1" into the "form1" .....
Not by creating new elements, but moving the "text1" and "hidden1"
elements into the form using javascript, after i click the submit
button....
So if i loop the "form1", i can get the values for "text1" and
"hidden1" too.....

Is that possible ?

Here is the source ....

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
<title>...</title>
<script type="text/javascript">
function submitIt()
{
var obj_text1 = document.getElementById("text1");
var obj_hidden1 = document.getElementById("hidden1");
var oFormObj = document.getElementById("form1");
for (var i=0;i<oFormObj.length;i++)
{
alert(oFormObj[i].name+" -- "+oFormObj[i].type)
}
}
</script>
</head>

<body>

<table border="0">
<tr>
<td>
<input type=text value='text111' name=text1 id=text1 >
<input type=hidden value='hidden111' name =hidden1 id=hidden1 >

<form id=form1 name=form1 action=test.html>

<input type=text value='text222' name =text2 id=text2 >
<input type=hidden value='hidden222' name =hidden2 id=hidden2>

<br>
<input type=button value=submit onClick="submitIt()">

</form>
</td>
</tr>
</table>

</body>
</html>

Dec 6 '05 #1
6 2277
fidodido wrote:
I was asked to do this....
Dynamically move "text1" and "hidden1" into the "form1" .....
Not by creating new elements, but moving the "text1" and "hidden1"
elements into the form using javascript, after i click the submit
button....
So if i loop the "form1", i can get the values for "text1" and
"hidden1" too.....

Is that possible ?
Why not just put them in the form in the first place? This seems
rather pointless, but anyway...

Here is the source ....
When posting code, please indent using 2 or 4 spaces, not tabs.


<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
<title>...</title>
<script type="text/javascript">
function submitIt()
Change this to (see below):

function submitIt(formObj)
{
var obj_text1 = document.getElementById("text1");
var obj_hidden1 = document.getElementById("hidden1");
var oFormObj = document.getElementById("form1");
You don't need to use getElementById for the form, just pass a
reference to the form from the submit button.
function submitIt(formObj)
{
var obj_text1 = document.getElementById("text1");
var obj_hidden1 = document.getElementById("hidden1");

formObj.appendChild(obj_text1.parentNode.removeChi ld(obj_text1))
formObj.appendChild(obj_hidden1.parentNode.removeC hild(obj_hidden1))
}
[...] </script>
</head>

<body>

<table border="0">
<tr>
<td>
<input type=text value='text111' name=text1 id=text1 >
<input type=hidden value='hidden111' name =hidden1 id=hidden1 >

<form id=form1 name=form1 action=test.html>

<input type=text value='text222' name =text2 id=text2 >
<input type=hidden value='hidden222' name =hidden2 id=hidden2>

<br>
<input type=button value=submit onClick="submitIt()">

If you want this to submit the form, make the button a submit button:

<input type="submit" value="Submit" onclick="submitIt();">
Attribute values should always be quoted, even though it's not always
necessary.

[...]
--
Rob
Dec 6 '05 #2
Thanx!!!! Thought it can't be done.. :)

Seriously i am not sure why I was asked to do this specific way....

Dec 6 '05 #3

Well, for any Form element in the page, there have to have a parent
FORM element, so, you cannot just drop an <input> element like so
without the browser, either tossing it or giving it a parent in
quirk-mode or sometimes in transitional DTD. You can add text as
argument to a property(innerHTML) of the <FORM> in order to produce
those 2 inputs, however, I'm assuming what you want is to produce is
to add these 2 new elements upon some user request, like [add extra
options] button or so, you can do so by including them from the getgo
in the Form with the rest of the elements, and toggle their
..style.display property or .style.visibility upon and onclick on a
link or a button or else.

Danny
Dec 6 '05 #4
If let say, by not using appendChild, is there any other way? coz if I
use appendChild, the layout will change...

I am not allowed to add any table tags...and the submit type has to be
button .....??

The source -

<html>
<head>
<title>...</title>
<script type="text/javascript">
function submitIt()
{
var obj_text1 = document.getElementById("text1");
var obj_hidden1 = document.getElementById("hidden1");
var formObj = document.getElementById("form1");
//formObj.appendChild(obj_text1.parentNode.removeChi ld(obj_text1))

//formObj.appendChild(obj_hidden1.parentNode.removeC hild(obj_hidden1))
for (var i=0;i<formObj.length;i++)
{
//alert(formObj[i].name+" -- "+formObj[i].type)
}
}
</script>
</head>
<body>
<input type=text value='text111' name=text1 id=text1>
<input type=hidden value='hidden111' name =hidden1 id=hidden1>
<form id=form1 name=form1>
<input type=text value='text222' name =text2 id=text2>
<input type=hidden value='hidden222' name =hidden2 id=hidden2>
<br>
<input type="button" value="Submit" onclick="submitIt();">
</form>
</body>
</html>

Dec 6 '05 #5
fidodido wrote:
If let say, by not using appendChild, is there any other way? coz if I
use appendChild, the layout will change...
You could instead use insertBefore with the form's firstChild. What
does it matter if the layout changes? It is done onsubmit, the user has
no further interaction with the form.

I am not allowed to add any table tags
The form is completely enclosed within a table cell, the suggested code
does not add any table elements, it just moves the two input elements
into the form.

...and the submit type has to be button .....??
Is that a statement or a question? As posted, the form has no submit
button. The 'submit' button does not attempt to call the form's submit
method, so the form will not be submitted.


The source -

<html>
<head>
<title>...</title>
<script type="text/javascript">
function submitIt()
{
var obj_text1 = document.getElementById("text1");
var obj_hidden1 = document.getElementById("hidden1");
var formObj = document.getElementById("form1");


The following will maintain the layout, provided there is nothing else
happening in the table or form.

formObj.insertBefore(
document.createElement('br'),
formObj.firstChild);
formObj.insertBefore(
obj_text1.parentNode.removeChild(obj_text1),
formObj.firstChild);
formObj.insertBefore(
obj_hidden1.parentNode.removeChild(obj_hidden1),
formObj.firstChild);
}
</script>

[...]

I still don't understand why the elements aren't included in the form in
the first place, then there would be no need for JavaScript, nor the
need to play with the document structure without modifying the presentation.
--
Rob
Dec 6 '05 #6
zif
Danny wrote:
Well, for any Form element in the page, there have to have a parent
There is only one 'form element', and that is denoted using the HTML
form tag set. Other elements may be form controls, but they aren't
'form elements'.

Any element that can be a form control (input, select, button, etc.) can
also be used outside a form. There is no requirement that they be in a
form.

FORM element, so, you cannot just drop an <input> element like so
without the browser, either tossing it or giving it a parent in
quirk-mode or sometimes in transitional DTD.
Complete rubbish.

You can add text as
argument to a property(innerHTML) of the <FORM> in order to produce
those 2 inputs, however, I'm assuming what you want is to produce is
to add these 2 new elements upon some user request, like [add extra


No, the intention was stated quite clearly that the OP wishes to move
the input elements into the table. Any use of innerHTML for that is
almost certain to fail in most browsers.
[... more rubbish snipped ...]
--
Zif
Dec 6 '05 #7

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

Similar topics

4
by: Terry | last post by:
I have a number of input boxes used to display totals based on selected items for each row in a table. There are more than a few rows that are identical, except for the form field name. I have...
1
by: Will | last post by:
Hi, I have a problem trying to validate dynamically created html form elements using javascript. I have dynamically created a check box using ASP for each record in a recordset and have given each...
10
by: Kathy Burke | last post by:
HI. in asp.net app, I have an xmlDocument that I transform to the client html. Using xsl I create a few textboxes to capture user input. Each of these are related to <data> elements in the xmlDoc....
4
by: Sandeep | last post by:
Hi I am doing one thing in my website ,actually i want to add controls dynamically to a web form and want to access the elements but when i postback the form,and access that control, it gives...
1
by: CS Wong | last post by:
Hi, I have a page form where form elements are created dynamically using Javascript instead of programatically at the code-behind level. I have problems accessing the dynamically-created...
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...
5
by: stellstarin | last post by:
I have a html where fields are created and added dynamically on the client side. I use the AppendChild() call to create fields dynamically. On submit i try to get the value for all the...
2
by: Rich | last post by:
Greetings. I have a child form inside an MID parent form. The child form is wider than the MID parent form. The child form contains a number of textboxes which are positioned from left to...
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...

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.