createElement with a name

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ivo

    #1

    createElement with a name

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

    var r=document.crea teElement('tr') ;
    var c=document.crea teElement('td') ;
    var cn=c.cloneNode( false);
    cn.className='t dname'; cn.appendChild( document.create TextNode(p));
    var c2=c.cloneNode( false);
    var i=document.crea teElement('inpu t');
    i.type='text'; i.size=8; i.className='it ext';
    var i2=i.cloneNode( true); i.name=p+'pas'; i2.name=p+'fun' ;
    c.appendChild(i );
    c2.appendChild( i2);
    r.appendChild(c n);
    r.appendChild(c );
    r.appendChild(c 2);
    var x=document.getE lementsByTagNam e('table')[0].rows;
    x=x[x.length-1]; // the one before the last
    x.parentNode.in sertBefore(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


  • Martin Honnen

    #2
    Re: createElement with a name



    Ivo wrote:
    [color=blue]
    > I have this code to add a row to a table:
    >
    > var r=document.crea teElement('tr') ;
    > var c=document.crea teElement('td') ;
    > var cn=c.cloneNode( false);
    > cn.className='t dname'; cn.appendChild( document.create TextNode(p));
    > var c2=c.cloneNode( false);
    > var i=document.crea teElement('inpu t');
    > i.type='text'; i.size=8; i.className='it ext';
    > var i2=i.cloneNode( true); i.name=p+'pas'; i2.name=p+'fun' ;
    > c.appendChild(i );
    > c2.appendChild( i2);
    > r.appendChild(c n);
    > r.appendChild(c );
    > r.appendChild(c 2);
    > var x=document.getE lementsByTagNam e('table')[0].rows;
    > x=x[x.length-1]; // the one before the last
    > x.parentNode.in sertBefore(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?[/color]

    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/de...ateelement.asp
    which indicates there are problems with the name attribute and
    unfortunately suggests you need to use the IE only extension
    document.create Element('<input type="text" name="inputName ">')

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/

    Comment

    • Ivo

      #3
      Re: createElement with a name

      "Martin Honnen" wrote[color=blue]
      > Ivo wrote:[color=green]
      > > var i=document.crea teElement('inpu t');
      > > i.type='text'; i.size=8; i.className='it ext';
      > > 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?[/color]
      >
      > If you have problems with a script and come here please tell us which
      > browser or browsers that problem occurs with.[/color]

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

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

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


      Comment

      Working...