Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Aug 2005
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Firefox - dynamically creating form elements

    Hi,

    I have the following function, which dynamically creates input boxes, one on top of each other.

    Code:
    <script type="text/javascript">
    function add() {
    
    var input = document.createElement("<input size=30 type=\"text\" name=\"extrachildren[]\">")
    var br = document.createElement("<br>");
    var br2 = document.createElement("<br>");
    document.getElementById("children").insertBefore(br);
    document.getElementById("children").insertBefore(br2);
    document.getElementById("children").insertBefore(input);
    
    }
    </script>
    It works fine in IE, however, it doesn't seem to work with Firefox. Nothing happens in Firefox when I call it. Is it possible to have a function that creates input boxes, that works cross-browser?

    Thanks in advance for any help.

  • #2
    Regular Coder
    Join Date
    Jul 2005
    Posts
    116
    Thanks
    0
    Thanked 0 Times in 0 Posts
    There are a few problems with your code. You shouldn't be passing HTML into createElement(). You only pass the tag name. Also, Firefox doesn't like it when you leave out the second parameter for insertBefore. You should be passing null.

    This should work for you:
    Code:
    <script type="text/javascript">
    function add()
    {
        var input = document.createElement('input');
        input.setAttribute('size', '30');
        input.setAttribute('type', 'text');
        input.setAttribute('name', 'extrachildren[]');
        var parent = document.getElementById('children');
        parent.insertBefore(document.createElement('br'), null);
        parent.insertBefore(document.createElement('br'), null);
        parent.insertBefore(input, null);
    }
    </script>

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    in fact why using?
    parent.insertBefore(document.createElement('br'), null);
    when it would be the same with
    parent.appendChild(document.createElement('br'));
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

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