I am trying to add onclick event handler to many objects but I can't understand why it doesn't work.
I would appreciate nothing too advanced. To assign event handler I use traditional approach as described in http://www.quirksmode.org/js/events_tradmod.html
Heres the code (extract.js):
Code JavaScript://the class function extract(){ this.iForm=document.getElementById('iNew') this.navSel=new Array() this.x=0 this.navSel[this.x]=new Array(3) this.navSel[this.x][0]=this.iForm.iVarrantyYears this.navSel[this.x][1]=document.getElementById('nSel') this.navSel[this.x][2]=document.getElementById('pSel') this.x++ this.navSel[this.x]=new Array(3) this.navSel[this.x][0]=this.iForm.bFloors this.navSel[this.x][1]=document.getElementById('nSel2') this.navSel[this.x][2]=document.getElementById('pSel2') this.selectNext=function(select){ if(select.selectedIndex<select.length){select.selectedIndex+=1} } this.selectPrevious=function(select){ if(select.selectedIndex>=1){select.selectedIndex-=1} } } //function to execute onload function prepare(){ o=new extract() var c=o.navSel.length for(i=0;i<c;i++){ //The following does NOT work o.navSel[i][1].onclick=function(){o.selectNext(o.navSel[i][0])} o.navSel[i][2].onclick=function(){o.selectPrevious(o.navSel[i][0])} } //The following approach works /* o.navSel[0][1].onclick=function(){o.selectNext(o.navSel[0][0])} o.navSel[0][2].onclick=function(){o.selectPrevious(o.navSel[0][0])} o.navSel[1][1].onclick=function(){o.selectNext(o.navSel[1][0])} o.navSel[1][2].onclick=function(){o.selectPrevious(o.navSel[1][0])} */ //But obviously I want to add event automatically }
This JavaScript code is used in extract.html (excerpt):
Code HTML4Strict:<html><head> <script type="text/javascript" src="extract.js"></script> </head><body onload="prepare()"> <form action="" method="post" id="iNew"> <p>Years of Varranty: <select name="iVarrantyYears" /></select> <input type="button" id="nSel" value="+" /> <input type="button" id="pSel" value="-" /></p> <p>Floor count in the building: <select name="bFloors" /></select> <input type="button" id="nSel2" value="+" /> <input type="button" id="pSel2" value="-" /></p> </form> </body></html>
I know that both select tags don't have options, but I generate them with JS because they hold sequential numbers and this part has no impact on the problem at hand.
Both functions help select next or previous index in a given select tag for greater comfort
Thanks!







Bookmarks