View Single Post
Join Date: May 2005
Posts: 182
Reputation: alpha_foobar is an unknown quantity at this point 
Solved Threads: 3
alpha_foobar's Avatar
alpha_foobar alpha_foobar is offline Offline
Junior Poster

Re: Urgent.....Dynamic Changes....

 
0
  #2
May 3rd, 2005
Hi,
I don' think you have worded your question very well. And I am not sure why you want to create this content in this manner (though perhaps its just POC and you'll reveal your true ideas later?).

Anyhow, I think that your problem is your understanding of the getElementByTagName function. This function returns a list of elements matching the argument name. In this case "SELECT". Even if there is only one such element, it will be returned in a list. So you'd need to retrieve it using the item method.. i.e. di.item(0).

I have edited your code, so I could get a feel for what you were after and how you were doing it. I think I've created something along the lines you were after, that is also a little simpler to read and follow.

It seemed sensible to me to pass the node that created the event as an argument to the function, this way you won't have to search for it when it gets there... apart from that; the only other difference is it runs in Mozilla as well... Which is a trend in almost all my code as Mozilla is much simpler to debug than IE.

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1.  
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  3. <html lang="en">
  4. <head>
  5. <title>DOM form elements</title>
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  7.  
  8. <script type="text/javascript">
  9. var rowCount = 0;
  10.  
  11. function appendtable(){
  12.  
  13. // get html elements
  14. var divElement = document.getElementById("divide");
  15.  
  16. // create a new table each time.
  17. var tableElement = document.createElement("table");
  18.  
  19. // create new row and cells
  20. var newTr = document.createElement("tr");
  21. var td0 = document.createElement("td");
  22. var td1 = document.createElement("td");
  23. var td2 = document.createElement("td");
  24. var td3 = document.createElement("td");
  25.  
  26. // create new select
  27. var oSelect = document.createElement("select");
  28.  
  29. // set onchange event for select box
  30. oSelect.setAttribute("onchange", "txtChange(this);");
  31.  
  32. // keep track of the id of the object for interest.
  33. oSelect.setAttribute("id", "txtchange" + rowCount++);
  34.  
  35. // create and add new option 0
  36. var oOption0 = document.createElement("OPTION");
  37. var t0 = document.createTextNode("Ferrari");
  38. oOption0.setAttribute("value", 4);
  39. oOption0.appendChild(t0);
  40. oSelect.appendChild(oOption0);
  41.  
  42. // create and add new option 1
  43. var oOption1 = document.createElement("OPTION");
  44. var t1 = document.createTextNode("Fessari");
  45. oOption1.setAttribute("value", 3);
  46. oOption1.appendChild(t1);
  47. oSelect.appendChild(oOption1);
  48.  
  49. // cretae text elements
  50. var txt1 = document.createElement("input");
  51. txt1.type="text";
  52. txt1.value="mamu";
  53. var txt2=document.createElement("input");
  54. txt2.type="text";
  55. txt2.value="Machan";
  56. var txt3=document.createElement("input");
  57. txt3.type="text";
  58. txt3.value="maplai";
  59.  
  60. // add content to the table.
  61. td0.appendChild(oSelect);
  62. td1.appendChild(txt1);
  63. td2.appendChild(txt2);
  64. td3.appendChild(txt3);
  65. newTr.appendChild(td0);
  66. newTr.appendChild(td1);
  67. newTr.appendChild(td2);
  68. newTr.appendChild(td3);
  69. tableElement.appendChild(newTr);
  70. divElement.appendChild(tableElement);
  71.  
  72. // only need to do this ugly hack for Internet Explorer.... don't know why. never had to do this with dynamic nodes before
  73. // must be some bug i have missed.
  74. if(window.event) {
  75. document.getElementById("divide").innerHTML = document.getElementById("divide").innerHTML;
  76. }
  77. }
  78.  
  79. // function requires a node object.
  80. function txtChange(noden) {
  81. alert("hi: '" + (noden?noden.id:"node is null") + "'");
  82.  
  83. var di = document.getElementsByTagName("select");
  84. //when i tried to get the selected value its showing error.
  85. // tag name function returns a node list.. not a single node.. so if only one,
  86. // then access using di.item(0)
  87.  
  88. // however using my code changes, there is no reason to establish the node here, as it is
  89. // being passed in as a parameter.
  90.  
  91.  
  92. }
  93. </script>
  94.  
  95. <style type="text/css">
  96. <!--
  97. /* cascading style sheet */
  98. body { font-family:verdana; }
  99. -->
  100. </style>
  101.  
  102. </head>
  103. <body>
  104.  
  105. <div id="divide">
  106.  
  107. <input type="button" onclick="appendtable()" value="Press me">
  108.  
  109. </div>
  110.  
  111. </body>
  112. </html>

enjoy.
Reply With Quote