SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jan 2005
    Location
    london
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    unable to reference form elements created in javascript

    Hi,
    I'm really stumped with this one so I hope someone can help. I have a DHTML menu that creates some form elements, and this seems to work ok in that they can be read following a submit. However, they need modifying later in the javascript (before the submit) but I can't seem to access them. To test it I have the following code:

    menuForm=document.getElementById('form1');

    element=document.createElement('input');
    element.name='mytest';
    menuForm.appendChild(element);

    if(menuForm.elements['mytest'])
    {
    alert("menuForm.elements['mytest'] exists");
    }
    else
    {
    alert("menuForm.elements[mytest] doesn't exist");
    }


    It shows as not existing even though I have just created it.

    Any suggestions much appreciated.

  2. #2
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    This seems to be an issue in IE6 (and probably also earlier versions), but it works properly in Mozilla Firefox (version 1.0). The input is added visually in both browsers, but it seems like it is not accessible from the menuForm.elements array in IE. I will perform some tests, and will post a solution here (if I find one, that is).

    Yours, Erik.
    ERIK RIKLUND :: Yes, I've been gone quite a while.

  3. #3
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Very weird indeed, though it can be accessed with:

    Code:
    if(menuForm.elements[0])
    {
        alert("exists!");
    }
    Though it will be accesed with your attemp if you change the name to id:
    Code:
    element.id = "mytest";
    You can also access it without the elements:
    Code:
    if(menuForm["mytest"]){}
    // or
    if(menuForm.mytest){}


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •