createElement with a name

I

Ivo

I have this code to add a row to a table:

var r=document.createElement('tr');
var c=document.createElement('td');
var cn=c.cloneNode(false);
cn.className='tdname'; cn.appendChild(document.createTextNode(p));
var c2=c.cloneNode(false);
var i=document.createElement('input');
i.type='text'; i.size=8; i.className='itext';
var i2=i.cloneNode(true); i.name=p+'pas'; i2.name=p+'fun';
c.appendChild(i);
c2.appendChild(i2);
r.appendChild(cn);
r.appendChild(c);
r.appendChild(c2);
var x=document.getElementsByTagName('table')[0].rows;
x=x[x.length-1]; // the one before the last
x.parentNode.insertBefore(r,x);

I 'm sure I 'm missing something obvious, the row is added correctly,
containing the expected cells each with the right input elements, but the
generated input elements don't have names. It is the same with inputs of
type checkbox. The classes are set correctly, but [element].name returns
undefined. Howcome and what can I do about it?
Thanks
Ivo
 
M

Martin Honnen

Ivo said:
I have this code to add a row to a table:

var r=document.createElement('tr');
var c=document.createElement('td');
var cn=c.cloneNode(false);
cn.className='tdname'; cn.appendChild(document.createTextNode(p));
var c2=c.cloneNode(false);
var i=document.createElement('input');
i.type='text'; i.size=8; i.className='itext';
var i2=i.cloneNode(true); i.name=p+'pas'; i2.name=p+'fun';
c.appendChild(i);
c2.appendChild(i2);
r.appendChild(cn);
r.appendChild(c);
r.appendChild(c2);
var x=document.getElementsByTagName('table')[0].rows;
x=x[x.length-1]; // the one before the last
x.parentNode.insertBefore(r,x);

I 'm sure I 'm missing something obvious, the row is added correctly,
containing the expected cells each with the right input elements, but the
generated input elements don't have names. It is the same with inputs of
type checkbox. The classes are set correctly, but [element].name returns
undefined. Howcome and what can I do about it?

If you have problems with a script and come here please tell us which
browser or browsers that problem occurs with.
And above I don't see where you test the name property, please show
when/where you are doing that.
IE has a documented restriction, see
http://msdn.microsoft.com/library/d...hor/dhtml/reference/methods/createelement.asp
which indicates there are problems with the name attribute and
unfortunately suggests you need to use the IE only extension
document.createElement('<input type="text" name="inputName">')
 
I

Ivo

Ivo said:
var i=document.createElement('input');
i.type='text'; i.size=8; i.className='itext';
var i2=i.cloneNode(true); i.name=p+'pas'; i2.name=p+'fun';

generated input elements don't have names. It is the same with inputs of
type checkbox. The classes are set correctly, but [element].name returns
undefined. Howcome and what can I do about it?

If you have problems with a script and come here please tell us which
browser or browsers that problem occurs with.

Yes sorry, I forgot. Testing in IE6/Win only sofar. Ultimately it should run
on IE/Mac as well...
And above I don't see where you test the name property, please show
when/where you are doing that.

There is no explicit test in the code as shown. It is a stripped down
version of a much larger script.
IE has a documented restriction, see
http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/ref
erence/methods/createelement.asp
which indicates there are problems with the name attribute and
unfortunately suggests you need to use the IE only extension
document.createElement('<input type="text" name="inputName">')

I see. Shucks. That is a helpful link.
Thanks
Ivo
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top