How to create HIDDEN Input element using DOM

Discussion in 'Javascript' started by Raghuram Banda, Nov 4, 2003.

  1. Hi All,

    Can any one help me how to create a HIDDEN element using JavaScript
    (DOM) dynamically
    The following codes works fine with IE but not in Netscape

    currentElement = document.createElement("input");
    currentElement.setAttribute("type", "hidden");
    currentElement.setAttribute("name", "hiddenName");
    currentElement.setAttribute("id", "hiddenName");
    currentElement.setAttribute("value", "14041978");
    currentCell.appendChild(currentElement);

    and I'm appending 'currentCell' to a table cell which was also created
    dynamically.

    Can any one pull me out of this problem

    Thanks in advance
    Raghuram Banda
     
    Raghuram Banda, Nov 4, 2003
    #1
    1. Advertising

  2. Raghuram Banda

    Grant Wagner Guest

    Raghuram Banda wrote:

    > Hi All,
    >
    > Can any one help me how to create a HIDDEN element using JavaScript
    > (DOM) dynamically
    > The following codes works fine with IE but not in Netscape
    >
    > currentElement = document.createElement("input");
    > currentElement.setAttribute("type", "hidden");
    > currentElement.setAttribute("name", "hiddenName");
    > currentElement.setAttribute("id", "hiddenName");
    > currentElement.setAttribute("value", "14041978");
    > currentCell.appendChild(currentElement);
    >
    > and I'm appending 'currentCell' to a table cell which was also created
    > dynamically.
    >
    > Can any one pull me out of this problem
    >
    > Thanks in advance
    > Raghuram Banda


    The reference to "currentCell" probably isn't what you think it is, since
    Gecko-based browsers include whitespace as text nodes in the DOM,
    something like:

    <tr id="myRow">
    <td></td>

    Could be accessed by document.getElementById('myRow').firstChild in IE,
    but in a Gecko-based browser (Mozilla, Safari, Firebird), all you'd get is
    the new line text node which follows the <tr>.

    Also, to prevent possible problems appending an input to a cell, and since
    the thing you're adding is just a hidden input anyway, why not just append
    it to either the body, or the form?

    <body>
    <form name="myForm">

    ....
    document.body.appendChild(currentElement);
    .... or ...
    document.forms['myForm'].appendChild(currentElement);

    --
    | Grant Wagner <>

    * Client-side Javascript and Netscape 4 DOM Reference available at:
    *
    http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html

    * Internet Explorer DOM Reference available at:
    *
    http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_entry.asp

    * Netscape 6/7 DOM Reference available at:
    * http://www.mozilla.org/docs/dom/domref/
    * Tips for upgrading JavaScript for Netscape 7 / Mozilla
    * http://www.mozilla.org/docs/web-developer/upgrade_2.html
     
    Grant Wagner, Nov 5, 2003
    #2
    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. SolarCanine
    Replies:
    2
    Views:
    337
    SolarCanine
    Sep 20, 2005
  2. Replies:
    14
    Views:
    190
  3. Iulian Ilea

    problem: create element in create element

    Iulian Ilea, Dec 14, 2006, in forum: Javascript
    Replies:
    4
    Views:
    207
    Iulian Ilea
    Dec 17, 2006
  4. hidden input type and the dom

    , Jul 4, 2007, in forum: Javascript
    Replies:
    7
    Views:
    183
    Richard Cornford
    Jul 5, 2007
  5. OccasionalFlyer
    Replies:
    6
    Views:
    264
    Garrett Smith
    Jul 29, 2009
Loading...

Share This Page