Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding fields and table rows 2

Status
Not open for further replies.

bvallet

Programmer
Apr 10, 2001
52
0
0
US
What I'm trying to do is add fields to a form in response to a user action (button click or whatever). In this case I don't know how many items will be added so I don't want to include 50 hidden rows and show them as needed. I want to physically add fields and table rows to a form. In searching this forum, the only thing I've found so far is this:

formname.elements[lastindex+1]=new element

But no other information on how to use it is available.

If anyone has any suggestions, I would appreciate it.
 
i've done this by using empty divs and filing them in as the user does things using innerHTML. it only works in msie though but i'm sure there is probably a way to do it using document.layers.layerName.document.write() in netscape then putting all the info together when the user submits the form.
 
i think it might be done with help of DOM..

here is a link:
read about DOM XML;

here is an example, it creates a tr, td & input but i can't set input properties :(
i'm not so very experienced with DOM; may be anybody else could tell us how to set name property of this created input tag (as well as type)

<html><head><title>create row</title>
<script>
<!--
function createRow(menuid){
var daRow;
dafe=document.createElement(&quot;input&quot;);
document.createAttribute(&quot;name&quot;)
document.createAttribute(&quot;type&quot;)
dafe.setAttribute(&quot;name&quot;,&quot;newtextfield&quot;);
dafe.setAttribute(&quot;type&quot;,&quot;text&quot;);
daRow = document.createElement(&quot;TR&quot;);
daTd = document.createElement(&quot;TD&quot;);
daTd.appendChild(dafe);
with(daRow){
appendChild(daTd);
}
document.getElementById(&quot;_tbody&quot;).appendChild(daRow);
}
//-->
</script>
</head>
<body>
<form name=my>
<input type=&quot;button&quot; value=&quot;create row&quot; onclick=&quot;createRow('aa')&quot;><br>
<input type=&quot;button&quot; value=&quot;html&quot; onclick=&quot;alert(document.body.innerHTML)&quot;><br>
</form>
<table >
<tbody id=&quot;_tbody&quot;>
<tr >
<td>old row</td>
</tr>
</tbody>
</table>
</body>
</html> Victor
 
the only problem is that netscape before 6 or 6.01 or something won't support that way.

object.name is supposedly supposed to set the nodes name in ie6 but...
The NAME attribute cannot be set at run time on elements dynamically created with the createElement method. To create an element with a name attribute, include the attribute and value when using the createElement method.

so you can use
document.createElement(&quot;<input NAME=\'&quot;+Name+&quot;\' type=\'&quot;+type+&quot;\'></A>&quot;);
 
yup, my solution - for DOM browsers only (meaning not for ie<5, ns<6, opera, etc)

& twist, thanks for tip :) Victor
 
you could also use something like this off the microsoft site to set attributes after the fact.

<SCRIPT>
function fnSetNamedItem(){
var nnm = myDIV.attributes;
var namedItem = document.createAttribute(&quot;title&quot;);
namedItem.value = &quot;This is a ToolTip&quot;;
nnm.setNamedItem(namedItem);
}
</SCRIPT>
 
oh, yea, and twist..

wellcome to TT :] (you're new here, right?) Victor
 
Thanks to both of you, you've given me alot to play with.

Now, on to another question that I discovered while trying this stuff:

Can you no longer escape double-quotes (i.e. var name = 'NAME=\&quot;test\&quot;';) in Javascript?

I tried to do that with this code and kept getting unterminated string constant errors. I know I used to be able to, (or at least I think I used to be able to) and my favorite reference book (O'reilly) says you can.

Any ideas? I know this is straying from the original post, but I'm curious.
 
yah you can do that, least in msie 6

<script>
var name = 'NAME=\&quot;test\&quot;'
alert(name)
</script>

works fine, you're probably missing a quotation somewhere and its making everything all messed up
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top