How do I add a control to a form.

M

Mike R

Hi,

I have a form frm1 . The form is generated by a "control" that is placed on
the page - I have no access to the source to modify the controls. I can
access other hidden controls within it by referencing:

ocontainer.frm1.txt1.value;

I would like to insert my own hidden control that can be posted back to to
my page so I can do something with it. How can I do this from clientside at
runtime?

Thanks

Mike
 
R

RobB

Mike said:
Hi,

I have a form frm1 . The form is generated by a "control" that is placed on
the page - I have no access to the source to modify the controls. I can
access other hidden controls within it by referencing:

ocontainer.frm1.txt1.value;

I would like to insert my own hidden control that can be posted back to to
my page so I can do something with it. How can I do this from clientside at
runtime?

Thanks

Mike

Quick guess:

var hfield = document.createElement('input');
hfield.setAttribute('type', 'hidden');
hfield.setAttribute('name', 'somename');
hfield.setAttribute('value', 'somevalue');
ocontainer.frm1.appendChild(hfield);
ocontainer.frm1.somename = hfield;
ocontainer.frm1.elements.somename = hfield;
 
M

Michael Winter

RobB wrote:

[snip]
var hfield = document.createElement('input');
hfield.setAttribute('type', 'hidden');
hfield.setAttribute('name', 'somename');
hfield.setAttribute('value', 'somevalue');

It's preferable to use the shortcut properties in HTML documents. That is,

hfield.type = 'hidden';

and so on.
ocontainer.frm1.appendChild(hfield);

The element that should be used a container may vary by DOCTYPE. A
Transitional document can use a FORM element directly. With Strict,
the container should be a block-level element within the form.
ocontainer.frm1.somename = hfield;
ocontainer.frm1.elements.somename = hfield;

That's unnecessary. Inserting a form control into a form automatically
adds it to the elements collection and sets the form property on the
control. This even occurs if the new element is not a child of the
form, but a descendant.

Mike
 
M

Mike R

RobB said:
Quick guess:

var hfield = document.createElement('input');
hfield.setAttribute('type', 'hidden');
hfield.setAttribute('name', 'somename');
hfield.setAttribute('value', 'somevalue');
ocontainer.frm1.appendChild(hfield);
ocontainer.frm1.somename = hfield;
ocontainer.frm1.elements.somename = hfield;

Thanks Rob and Mike, I will give it a go
 
M

Mike R

RobB said:
Quick guess:

var hfield = document.createElement('input');
hfield.setAttribute('type', 'hidden');
hfield.setAttribute('name', 'somename');
hfield.setAttribute('value', 'somevalue');
ocontainer.frm1.appendChild(hfield);
ocontainer.frm1.somename = hfield;
ocontainer.frm1.elements.somename = hfield;

Hi

I get invalid argument with this ocontainer.frm1.appendChild(hfield);

my code :

var oGridContainerWindow =
document.getElementById("crmGrid0").contentWindow;

var hfield = document.createElement('input');

hfield.type = 'hidden';

hfield.id = 'rowHeight';


oGridContainerWindow.frmGrid.appendChild(hfield);
 
R

RobB

Hi Mike.

Michael said:
RobB wrote:

[snip]
var hfield = document.createElement('input');
hfield.setAttribute('type', 'hidden');
hfield.setAttribute('name', 'somename');
hfield.setAttribute('value', 'somevalue');

It's preferable to use the shortcut properties in HTML documents. That is,

hfield.type = 'hidden';

and so on.

Hmm..I demur. I think it's preferable to use DOM Level 1+ methods
whenever feasible, avoiding them where cross-browser implementation is
uneven (and there's a better way). setAttribute() for these cases is
reliable in my experience.
The element that should be used a container may vary by DOCTYPE. A
Transitional document can use a FORM element directly. With Strict,
the container should be a block-level element within the form.

Just conforming to the OPs description.
That's unnecessary. Inserting a form control into a form automatically
adds it to the elements collection and sets the form property on the
control. This even occurs if the new element is not a child of the
form, but a descendant.

Not intended to add it to elements[], just to create the usual
references at Form.element_name and Form.elements.element_name, not
implemented in current versions of MSIE.
 
M

Mike R

Mike R said:
Hi

I get invalid argument with this ocontainer.frm1.appendChild(hfield);

my code :

var oGridContainerWindow =
document.getElementById("crmGrid0").contentWindow;

var hfield = document.createElement('input');

hfield.type = 'hidden';

hfield.id = 'rowHeight';


oGridContainerWindow.frmGrid.appendChild(hfield);

Ignore this, I used var hfield =
oGridContainerWindow.document.createElement('input');

and that worked
 
R

RobB

Mike R wrote:

(snip)
Ignore this, I used var hfield =
oGridContainerWindow.document.createElement('input');

and that worked

Very sneaky, leaving out the part about the iframe. #;=)

Glad you got it sorted out.
 
M

Michael Winter

RobB wrote:

[setAttribute vs. convenience properties]
I think it's preferable to use DOM Level 1+ methods whenever feasible,

On what grounds? The convenience properties were provided explicitly
for HTML documents to provide a simple and efficient method of
altering the attributes of HTML elements.

If /true/ XHTML were an issue, and it shouldn't be for a long time
yet, there would be little option than to use methods. However, given
the opportunity, why not take the shorter route?

[snip]
[snip]

not implemented in current versions of MSIE.

You know, I never noticed that. The inserted elements do exist within
the elements collection, but not as named properties. How odd. I had
noticed problems with other collections - for example, the links
collection which doesn't expose any named properties - but I didn't
think it extended to forms.

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top