Validate document.createElement

D

Dave

I have a page which uses JavaScript to create form elements using
document.createElement('input'), etc.. Both Firefox and IE have no
problem accomplishing this and when the form is submitted all the
information is passed correctly.

I am now trying to validate the form using JavaScript when the page is
submitted. Firefox has no problems with this but IE returns
'document.form1.*THE FORM FIELD*.value is null or not an object' for
the elements that were created using document.createElement when I try
get their value using 'document.form1.*THE FORM FIELD*.value'. How do
I get the value of the form elements that were created using
JavaSCript?

(BTW: The form name is form1).
 
F

Fred Oz

Dave wrote:
[...]
I am now trying to validate the form using JavaScript when the page is
submitted. Firefox has no problems with this but IE returns
'document.form1.*THE FORM FIELD*.value is null or not an object' for
the elements that were created using document.createElement when I try
get their value using 'document.form1.*THE FORM FIELD*.value'. How do
I get the value of the form elements that were created using
JavaSCript?

Without seeing a bit of code, how can we tell? Just post a minimal,
concise example that demonstrates your problem or provide a URL to
same.

Say a bit that creates an element gives it appropriate attributes and
adds it to the document, and another bit that tries to read its value
or text or whatever.

At a guess, I'd say the issue is with the way you are creating the
attribute you are using to reference the form element - check the id
and/or name you are assigning it.

Fred.
 
M

Martin Honnen

Dave said:
I have a page which uses JavaScript to create form elements using
document.createElement('input'), etc.. Both Firefox and IE have no
problem accomplishing this and when the form is submitted all the
information is passed correctly.

I am now trying to validate the form using JavaScript when the page is
submitted. Firefox has no problems with this but IE returns
'document.form1.*THE FORM FIELD*.value is null or not an object' for
the elements that were created using document.createElement when I try
get their value using 'document.form1.*THE FORM FIELD*.value'. How do
I get the value of the form elements that were created using
JavaSCript?

That is a known problem with IE (on Win at least). What kind of input
control is that? If it is a text or password or select or textarea where
you usually use a unique name you can help yourself by also setting the
id of that element e.g.

var input = document.createElement('input');
input.type = 'text';
input.id = 'inputName';
input.name = 'inputName';
document.forms[0].appendChild(input);

then the element is accessible as

document.forms[0].inputName

or as

document.forms[0].elements.inputName

That approach fails of course if you want to create a group of radio
buttons which need to have the same name but are not allowed to have the
same id but event then you can help yourself by using an id attribute,
just make sure you assign different ids to the different radio buttons
and then access elements as
document.forms[0].elements.radioButtonId

If you are desperate you can also use the IE only approach
var input =
document.createElement('<input type="radio" name="radioName" value="Kibo">')
that way IE manages to update the form.elements collection properly.
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top