472,348 Members | 1,198 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,348 software developers and data experts.

Inserting textnode or table inside FORM tag

I have a form on a page that has several textareas, and textboxes
inside a table (so the table containing the textboxes is also inside
the FORM tag).

I want to replace the textareas with simple text instead. But I want
to keep the format of my page EXACTLY the same. However, the problem
is that ...

1) Javascript won't let me create say a one-cell TABLE containing some
text (e.g. textarea's value) and then insertBefore an element in the
form. This is so because the a TABLE element is not compatible to be a
FORM's child.

2) I surely can insert a text node or a table using appendChild or
insertBefore on document.body. However, like I said I must maintain
the formatting of my page, so I again can not do this. Reason being
that the document.body won't have access to anything that's inside the
FORM tag and can only insert before or after the FORM tag.

e.g.

<BODY>
<P id="para1">foo</P>
<FORM id=form1">
<!-- anything in here is Form's property NOT body's -->
<input type="text" name="name" id="txtbox1">
</FORM>
</BODY>

So the following code is invalid:

var tNode = document.createTextNode ("hello");
document.body.insertBefore (tNode,
document.forms[0].getElementsByTagName ("txtbox1"));

because I can't insert a textnode before a form element using a body
method.
And following code is valid but not what I want according to my second
point above:

var tNode = document.createTextNode ("hello");
document.body.appendChild (tNode);
How can I insert a text node or table containing text inside FORM
tags?
Jul 23 '05 #1
2 2797
Asad wrote:
I have a form on a page that has several textareas, and
textboxes inside a table (so the table containing the
textboxes is also inside the FORM tag).
As it must be.
I want to replace the textareas with simple text instead.
if(document.createTextNode && document.getElementById){
var tNode = document.createTextNode ("hello");
var txtArea = document.getElementById("txtbox1");
if((tNode)&&
(txtArea)&&
(txtArea.parentNode)&&
(txtArea.parentNode.replaceChild)
){
txtArea.parentNode.replaceChild(tNode, txtArea);
}
}
But I want to keep the format of my page EXACTLY the
same. However, the problem is that ... 1) Javascript won't let me create say a one-cell TABLE
containing some text (e.g. textarea's value) and then
insertBefore an element in the form. This is so because
the a TABLE element is not compatible to be a
FORM's child.
Tables are completely compatible with being a child of a form. You are
getting confused about the DOM tree structure. The form control
(textarea) within a table is a descendent of the form, but it is a child
of its containing TD element, and it would be the insertBefore method of
that TD element that would need to be called in order to insert anything
before the textarea. (Though your stated goal is to replace the textarea
so insertBefore doesn't sound like the method to be using).
2) I surely can insert a text node or a table using
appendChild or insertBefore on document.body. However,
like I said I must maintain the formatting of my page,
so I again can not do this. Reason being that the
document.body won't have access to anything that's inside
the FORM tag and can only insert before or after the FORM
tag.
In your example the only child of the BODY is the FORM. The TEXTAREA is
a child of the FORM.
e.g.

<BODY>
<P id="para1">foo</P>
<FORM id=form1">
<!-- anything in here is Form's property NOT body's -->
<input type="text" name="name" id="txtbox1">
</FORM>
</BODY>

So the following code is invalid:

var tNode = document.createTextNode ("hello");
document.body.insertBefore (tNode,
document.forms[0].getElementsByTagName ("txtbox1")); ^^^^^^^^^^^^^^^^^^^^
The getElementsByTagName method returns a - nodeList - object not a
Node, it is expecting its argument to be a tag name, which may have been
"TEXTAREA" in this case.

But more likely you should refer to an IDed textarea with:-

document.getElementById("txtbox1")

- or -

document.forms[0].elements['txtbox1']

- or -

document.forms['form1'].elements['txtbox1']

If you insist on using getElementsByTagName you need to pass the tag
name as an argument (eg 'TEXTAREA') and then index the textarea element
in question from the returned nodeList:-

document.forms[0].getElementsByTagName ("TEXTAREA")[0]

Then you have the problem with using the insertBefore method on the BODY
element. The textarea is not a child of the BODY element so that method
is specified as throwing a NOT_FOUND_ERR exception when it turns out
that its second argument is not one of its children.
because I can't insert a textnode before a form element
using a body method.
You probably can, but you don't appear to have tried to do that yet, and
it is not what you want to do anyway.
And following code is valid but not what I want according
to my second point above:

var tNode = document.createTextNode ("hello");
document.body.appendChild (tNode);
How can I insert a text node or table containing text
inside FORM tags?


Using the insertBefore or appendChild methods of the FORM element, but
that is still not what you want to do. To replace the textarea Element
with a text Node use the code I listed above (first).

Richard.
Jul 23 '05 #2
"Richard Cornford" <ri*****@litotes.demon.co.uk> wrote in message
news:c9**********@hercules.btinternet.com...
Asad wrote:
I have a form on a page that has several textareas, and
textboxes inside a table (so the table containing the ^^^^^^^^^ textboxes is also inside the FORM tag). <snip> <BODY>
<P id="para1">foo</P>
<FORM id=form1">
<!-- anything in here is Form's property NOT body's -->
<input type="text" name="name" id="txtbox1">
</FORM>
</BODY> <snip> document.forms[0].getElementsByTagName ("txtbox1"));
<snip> document.forms[0].getElementsByTagName ("TEXTAREA")[0]

<snip>
For text boxes (<input type="text">) that would be:-

document.forms[0].getElementsByTagName ("INPUT")[0]

-everything else would be the same.

Richard.
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Marko Poutiainen | last post by:
Situation: We had to make our SQLServer 2000 database multi-lingual. That is, certain things (such as product names) in the database should be...
12
by: Anna | last post by:
Hi all, I posted the same question this afternoon but my message isn't showing up, so I thought I'd give it another try.... in case you should...
8
by: L Major | last post by:
Hi Unfortunately, I am limited to using tables for part of my current project. I have a form that spans across a number of TR and TD in the...
1
by: Ben | last post by:
Hi all, I'm trying to write inside a table cell from external javascript but am not successful. When I insert inside a form within <td...>, it...
4
by: cassey14 | last post by:
Hi! I am having a problem here..I have a form and in that form I have a subform..Inside that subform it has data..I wanted to insert that data in...
5
by: bob44 | last post by:
Hi, I recently created a mysql database using phpmyadmin. I then proceeded to make a form to insert data into the database, but the problem is that...
10
by: jmartmem | last post by:
Greetings, I have an ASP page with a 5x5 table embedded inside an Insert Record Form. This table contains several fields (mostly drop down list...
2
by: AlexanderDeLarge | last post by:
Hi! I got a problem that's driving me crazy and I'm desperately in need of help. I'll explain my scenario: I'm doing a database driven site...
1
by: javediq143 | last post by:
Hi All, This is my first post in this forum. I'm developing a CMS for my latest website. This CMS is also in PhP & MySQL. I'm done with the ADD...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.