Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #17404

Re: Problem w/ creating dynamic TR and its TDs

Message-ID <5462881.fXVs31Ce0e@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2012-12-02 03:46 +0100
Subject Re: Problem w/ creating dynamic TR and its TDs
Newsgroups comp.lang.javascript
References <e4c1bc98-b6a5-4142-a7a7-3fff318a7057@l12g2000vbj.googlegroups.com> <k9e45v$d6v$1@speranza.aioe.org>
Followup-To comp.lang.javascript

Followups directed to: comp.lang.javascript

Show all headers | View raw


Cezary Tomczyk wrote:

> W dniu 2012-12-02 00:17, justaguy pisze:
>> I've done such dynamic creation of TR and its TDs a while ago... but
>> runs into problem.
>> The thought is, if I have a current TR with ID of "row1", then the
>> following code should be able to create a new TR and its TDs and
>> append them to the "row1" Row.
>>
>> HTML:
>> <table>
>> <tr id="row1">
>>    <td><input type="text" id="myname" name="myname"><td>
>>    <td><input type="text" id="mydesc" name="mydesc"><td>
>> </tr>
>> </table>
>>
>> Javascript:
>> // find or define current row
>> var currentRow = "row1">
>>
>> // create new Row and Cells
>> var cnt = 1;
>> var newrow = document.createElement("tr");
>> var cell = document.createElement("td");
>> var cellText = document.createTextNode("<input type=text
>> name=partnumber"+cnt+ "id=partnumber"+cnt+ "class=autocomplete>");
>> cell.appendChild(cellText);
>> newrow.appendChild(cell);
>>
>> // now append the new Row and its Cells to current row
>> currentRow.appendChild(newrow);
>>
>> Question, that line of
>> var cellText = document.createTextNode("<input type=text
>> name=partnumber"+cnt+ "id=partnumber"+cnt+ "class=autocomplete>");
>> is wrong, that is, it's a text node, not an input node, and I don't
>> think there's something like createInputNode something, so, how do we
>> do it?
> 
> You can use something like this as a workaround for old IE and problems
> with "input type name":

There is no such problem.  You are confusing this with changing the value of 
the `type' property of an `input' element object after the corresponding 
element has been appended.
 
> elm - node name
> attributes - object with atttributes
> 
> var createElement = function(elm, attributes){
>      var div = document.createElement("div"),

Unnecessary; potentially harmful here (a `div' element in a `td' element).

>          element = document.createElement(elm);

OK.
 
>      for(var p in attributes){

Iterates over enumerable properties of `attributes'.  Better:

  for (var keys = Object.keys(attributes), i = 0, len = keys.length;
       i < len;
       ++i)

or pre-ES 5.1 variants thereof.  See also jsx.object.getKeys().

>        element[p]= attributes[p];

Fails to account for the mapping of attribute names that are reserved words 
to special attribute properties (like "for" → "htmlFor"), and of the `style' 
attribute.  See also jsx.dom.setAttr().

>      }
> 
>      div.appendChild(element);
>      return div.firstChild;

I beg your pardon?  What exactly are you creating the `div' element for if 
you are not returning a reference to it?  Mystical incantation?

> };
> 
> This is just an example and probably code could be more improved.

It is a solution looking for a problem while creating more problems than it 
fails to solve.   The correct way to address the problem actually posed by 
the question (as opposed to your answer) is:

  var input = document.createElement("input");
  input.name = "partnumber" + cnt;
  input.id = "partnumber" + cnt;
  input.className = "autocomplete";
  cell.appendChild(input);


PointedEars
-- 
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Problem w/ creating dynamic TR and its TDs justaguy <lichunshen84@gmail.com> - 2012-12-01 15:17 -0800
  Re: Problem w/ creating dynamic TR and its TDs Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2012-12-02 00:34 +0100
    Re: Problem w/ creating dynamic TR and its TDs justaguy <lichunshen84@gmail.com> - 2012-12-01 17:06 -0800
    Re: Problem w/ creating dynamic TR and its TDs Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-12-02 03:46 +0100
      Re: Problem w/ creating dynamic TR and its TDs justaguy <lichunshen84@gmail.com> - 2012-12-01 19:27 -0800
        Re: Problem w/ creating dynamic TR and its TDs Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-12-03 22:53 +0100
      Re: Problem w/ creating dynamic TR and its TDs Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2012-12-02 08:43 +0100
        Re: Problem w/ creating dynamic TR and its TDs Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-12-02 16:11 +0100
          Re: Problem w/ creating dynamic TR and its TDs Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2012-12-02 18:48 +0100
            Re: Problem w/ creating dynamic TR and its TDs Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-12-02 19:35 +0100
              Re: Problem w/ creating dynamic TR and its TDs Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-12-02 19:39 +0100

csiph-web