Validate document.createElement

Discussion in 'Javascript' started by Dave, Nov 5, 2004.

  1. Dave

    Dave Guest

    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).
     
    Dave, Nov 5, 2004
    #1
    1. Advertising

  2. Dave

    Fred Oz Guest

    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.
     
    Fred Oz, Nov 5, 2004
    #2
    1. Advertising

  3. Dave wrote:
    > 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.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Nov 5, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Weston C

    document.createElement('a') doesn't work?

    Weston C, Jan 9, 2004, in forum: Javascript
    Replies:
    4
    Views:
    115
    Michael Hill
    Jan 9, 2004
  2. Pieter Van Waeyenberge

    'wrap' inconsistency? createElement VS document.write

    Pieter Van Waeyenberge, Feb 9, 2004, in forum: Javascript
    Replies:
    3
    Views:
    190
    Pieter Van Waeyenberge
    Feb 11, 2004
  3. oeyvind toft

    document.createElement

    oeyvind toft, Sep 4, 2004, in forum: Javascript
    Replies:
    4
    Views:
    149
    Michael Winter
    Sep 5, 2004
  4. Andrew Poulos

    IE5 and document.createElement

    Andrew Poulos, Dec 6, 2004, in forum: Javascript
    Replies:
    2
    Views:
    120
    Andrew Poulos
    Dec 6, 2004
  5. James Black
    Replies:
    10
    Views:
    294
    Thomas 'PointedEars' Lahn
    May 25, 2006
Loading...

Share This Page