| Home | Forums | Reviews | Guides | Newsgroups | Register | Search |
![]() |
| Thread Tools |
| patrickkellogg |
|
|
|
| |
|
Martin Honnen
Guest
Posts: n/a
|
patrickkellogg wrote: > var position = document.getElementById('positions'); > tr = document.createElement("tr"); > position.appendChild(tr); You need to append the tr element to a tbody element (which the HTML parser creates even if it is not in the markup): var tbody = position.tBodies[position.tBodies.length - 1]; tbody.appendChild(tr); That makes IE happy and the other browsers too. -- Martin Honnen http://JavaScript.FAQTs.com/ |
|
|
|
|
|||
|
|||
| Martin Honnen |
|
|
|
| |
|
patrickkellogg
Guest
Posts: n/a
|
Martin,
Thanks very much. ie works much better but still doesn't display the select box years, which firefox and opera does okay. taking your advise, here is the function code: I am new at this dynamic stuff and i really don't understand it very well. did i put the: var tbody = position.tBodies[position.tBodies.length - 1]; tbody.appendChild(tr); in the correct place? Does it need to be inserted in more than one place? thanks again. patrick --------------------------------------------------------------------------- function addStuff() { var theValue = document.getElementById('theValue'); theValue.value += 1; var position = document.getElementById('positions'); tr = document.createElement("tr"); position.appendChild(tr); var tbody = position.tBodies[position.tBodies.length - 1]; tbody.appendChild(tr); td = document.createElement("td"); tr.appendChild(td); td.innerHTML = "<textarea name='emp" + theValue.value + "'></textarea>"; td = document.createElement("td"); tr.appendChild(td); td.innerHTML = "<P>From:</P><P>To:</P>"; sel = document.createElement("select"); sel.setAttribute("name", "aaa"); tr.appendChild(sel); for (n=1960; n < 2006; n++) { option = document.createElement("option"); option.setAttribute("value", n); option.innerHTML = n; sel.appendChild(option); } } ------------------------------------------------------------------------------------------------ Martin Honnen wrote: > patrickkellogg wrote: > > > > var position = document.getElementById('positions'); > > tr = document.createElement("tr"); > > position.appendChild(tr); > > You need to append the tr element to a tbody element (which the HTML > parser creates even if it is not in the markup): > var tbody = position.tBodies[position.tBodies.length - 1]; > tbody.appendChild(tr); > That makes IE happy and the other browsers too. > > > > -- > > Martin Honnen > http://JavaScript.FAQTs.com/ |
|
|
|
|
|||
|
|||
| patrickkellogg |
|
RobG
Guest
Posts: n/a
|
patrickkellogg wrote:
> Martin, Please dont' top post, interleave replies with trimmed, quoted text from whatever you are replying to. Also, don't use tabs for indenting when posting, use 2 or 4 spaces. > Thanks very much. ie works much better but still doesn't display the > select box years, which firefox and opera does okay. > > taking your advise, here is the function code: I am new at this > dynamic stuff and i really don't understand it very well. did i put > the: > > var tbody = position.tBodies[position.tBodies.length - 1]; > tbody.appendChild(tr); > > in the correct place? Does it need to be inserted in more than one > place? > > thanks again. > patrick > > > --------------------------------------------------------------------------- > > function addStuff() > { > var theValue = document.getElementById('theValue'); In the code you posted, there is no element with id 'theValue', is it an input element? > theValue.value += 1; If it is, then its value is a string and the above will concatenate a '1' to the end of it (1 will become 11, 20 will become 201, and so on), it wil not perform an arithmetic sum which is probably what you want. If you want to add one to the value you need to convert it to a number first. The unary '+' operator is normally suggested: theValue.value = +theValue.value + 1; Alternatively you can subtract -1 which is effectively the same as the above, but that is a little less logical and may create a maintenance issue: theValue.value -= -1; > var position = document.getElementById('positions'); > tr = document.createElement("tr"); > position.appendChild(tr); As noted previoiusly, most browsers let you add tr elements directly to the table (they will append them to the first tableSection element) but not IE. Your next couple of lines actually moves the row to the first tBody if it hand't been placed there already. > var tbody = position.tBodies[position.tBodies.length - 1]; > tbody.appendChild(tr); You can save all this fuss using insertRow: var tr = position.insertRow(-1); Done. The new row is appended as the table's last row and 'tr' is a reference to it. In IE you can omit the (-1), but not W3C compliant browsers, they need it (as per the spec). <URL:http://developer.mozilla.org/en/docs/DOM:table.insertRow> > td = document.createElement("td"); > tr.appendChild(td); And here you could use insertCell (and use 'var' to keep variables local unless you mean to use global scope, in which case they should be decared there for tidiness): var td = tr.insertCell(-1); > td.innerHTML = "<textarea name='emp" + theValue.value + > "'></textarea>"; You started off wanting to use XHTML, I guess you've seen the light and switched to HTML. You could use DOM here if you like: var tArea = document.createElement('textarea'); tArea.name = 'emp' + theValue.value; td.appendChild(tArea); > td = document.createElement("td"); > tr.appendChild(td); > td.innerHTML = "<P>From:</P><P>To:</P>"; Here you can use: td = tr.insertCell(-1); var p = document.createElement('p'); p.appendChild(document.createTextNode('From:')); td.appendChild(p); p = document.createElement('p'); p.appendChild(document.createTextNode('To:')); td.appendChild(p); > sel = document.createElement("select"); var sel = ... > sel.setAttribute("name", "aaa"); It's more reliable to use: sel.name = 'aaa'; setAttribute is broken in some circumstances in IE, so just don't use it. > tr.appendChild(sel); A TR can only have TD elements as children, you have to put the select in a TD or outside the table. I think you meant to do: td.appendChild(sel); > for (n=1960; n < 2006; n++) > { > option = document.createElement("option"); > option.setAttribute("value", n); > option.innerHTML = n; > sel.appendChild(option); As documented here many times, IE doesn't like creating options using createElement. Use DOM 0 and new Option (search for it, there are plenty of examples): for (n=1960; n < 2006; n++) { sel.options[sel.length] = new Option(n, 'value'); Which is much shorter too > } > } > Here's a working example (tested in IE 6 and Firefox 1.5): <title>Add stuff</title> <script type="text/javascript"> function addStuff() { var theValue = document.getElementById('theValue'); theValue.value = +theValue.value + 1; // theValue.value -= -1; var position = document.getElementById('positions'); var tr = position.insertRow(-1); var td = tr.insertCell(-1); var tArea = document.createElement('textarea'); tArea.name = 'emp' + theValue.value; td.appendChild(tArea); td = tr.insertCell(-1); var p = document.createElement('p'); p.appendChild(document.createTextNode('From:')); td.appendChild(p); p = document.createElement('p'); p.appendChild(document.createTextNode('To:')); td.appendChild(p); var sel = document.createElement("select"); sel.name = 'aaa'; td.appendChild(sel); for (n=1960; n < 2006; n++) { sel.options[sel.length] = new Option(n, 'value'); } } </script> <table id="positions"> <tr> <td><input type="text" value="0" id="theValue"></td> <td><input type="button" value="Add stuff" onclick="addStuff();"></td> </tr> </table> -- Rob |
|
|
|
|
|||
|
|||
| RobG |
|
|
|
| |
![]() |
| Thread Tools | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Adding Html Code dynmically in Placeholder | _MC_ | ASP .Net | 2 | 04-06-2007 03:21 PM |
| How to add javascript code dynmically? | cotton_gear | Javascript | 4 | 10-27-2005 10:45 AM |
| Re: dynmically show thumbnails | Brock Allen | ASP .Net | 0 | 04-14-2005 02:15 PM |
| [py2exe.i18n] English works, German works, but not French. What do I miss? | F. GEIGER | Python | 3 | 08-06-2004 10:01 AM |
| After rebooting my PC works, works, works! Antivirus problem? | Adriano | Computer Information | 1 | 12-15-2003 05:30 AM |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc..
SEO by vBSEO ©2010, Crawlability, Inc. |




