How to create HIDDEN Input element using DOM

R

Raghuram Banda

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
 
G

Grant Wagner

Raghuram said:
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 <[email protected]>

* 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
 

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,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top