|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
DOM/Adding elements and events
Been a while, have this issue with DOm and not sure if it is possible or not
This is where the basis cames from http://javascriptkit.com/javatutors/dom2.shtml and here is my code. Two issues - 1) the set attrribute for name does not work, why not I do not know but when I show the innerHTML it is not even listed. 2) the on change even is showing when I list the innerHTML but it does not trigger, why not? any way to make it work, if not the concept is totally useless to me. (main point) (I know php can easily write out the form but I wanted something different. It all works except for this stuff. Also this is test code so there is only a litle data in here) Thanks in advance. <html> <head> <script language="javascript"> var myitems = new Array(100); myitems[0] = new Array(100); myitems[0][0] = new Array(2); myitems[0][0][0] = "Main Numbers"; myitems[0][0][1] = "none"; myitems[0][1] = new Array(2); myitems[0][1][0] = "something"; myitems[0][1][1] = "wow"; myitems[1] = new Array(100); myitems[1][0] = new Array(2); myitems[1][0][0] = "Other Numbers"; myitems[1][0][1] = "none"; myitems[2] = new Array(100); myitems[2][0] = new Array(2); myitems[2][0][0] = "Links"; myitems[2][0][1] = "none"; myitems[3] = new Array(100); myitems[3][0] = new Array(2); myitems[3][0][0] = "Phone Numbers"; myitems[3][0][1] = "none"; function loaddata() { a = 0; while (myitems[a] != undefined) { myselect = document.createElement('select'); myselect.setAttribute('id',myitems[a][0][0]); myselect.setAttribute('name',myitems[a][0][0]); myselect.setAttribute('size', 1); myselect.setAttribute('onChange','alert('test');') ; b = 0; while (myitems[a][b] != undefined) { myselect.options[myselect.length] = new Option(myitems[a][b][0], myitems[a][b][1]); b++; } document.getElementById('databuddy').appendChild(m yselect); a++; } alert(document.body.innerHTML) } function go(mystr) { alert(mystr); } </script> </head> <body onload="loaddata();"> <form name="databuddy" id="databuddy"> </form> </body> </html> |
|
#2
|
|||
|
|||
|
RE: DOM/Adding elements and events
This appears to be another unfortunate IE issue. Your code functions correctly as-is under FF. It seems that if you use
Code:
myselect.setAttribute('NAME',myitems[a][0][0]);
it will handle the name issue. For the onchange I looked around a bit and found this: http://www.codecomments.com/archive298-2004-7-244684.html which leads to: http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/attachevent.asp I can't offer a fix, but hopefully this gets you headed in the right direction... |
|
#3
|
|||
|
|||
|
RE: DOM/Adding elements and events
well the name bit works, uppercase geez!
anyways on to the new stuff myselect = document.createElement('select'); myselect.setAttribute('id',myitems[a][0][0]); myselect.setAttribute('NAME',myitems[a][0][0]); myselect.setAttribute('size', 1); myselect.attachEvent('onChange','go'); my middle part now reads and I can't make it work. I get type mismatch errors. I also tried addEventListener that failed. The go function exists, I will have to look again tomorrow. I am out of time now. Thanks for the help. |
|
#4
|
|||
|
|||
|
RE: DOM/Adding elements and events
finally got it solved, I include just the javascript part here
some noticable points - 1) as noted the name attribute has tio be uppercase 2) onchange has to be LOWERcase not onChange 3) the function name does not have quotes around it in the attachEvent 4) I also inlude how to figure out what item called this because uou can't pass a parameter to the function in the attachEvent call. Thanks to all who helped. function loaddata(parentobj) { a = 0; while (myitems[a] != undefined) { myselect = document.createElement('select'); myselect.setAttribute('id',myitems[a][0][0]); myselect.setAttribute('NAME',myitems[a][0][0]); myselect.setAttribute('size', 1); myselect.attachEvent('onchange', go); b = 0; while (myitems[a][b] != undefined) { myselect.options[myselect.length] = new Option(myitems[a][b][0], myitems[a][b][1]); b++; } parentobj.appendChild(myselect); a++; } } function go() { ob = event.srcElement; alert(ob.value); } |
|
#5
|
|||
|
|||
|
RE: DOM/Adding elements and events
Cool. Good stuff to know...
|
![]() |
| Viewing: Codewalkers Forums > Other Technologies > Client Side Things > DOM/Adding elements and events |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|