PDA

View Full Version : Script works in Mozilla but not in IE



daheri
02-02-2006, 12:29 PM
Hi,
I'm currently developing a questionare web page for showing questions and handling response. The page will have 7 radiobuttons on a scale 1-7 for user response.
I want the user to be able to use the keyboard when answering by pressing the keys 1-7, or using the arrow keys. When the 1-7 key is pressed the corresponding radiobutton will be focused and checked. When the arrowkeys is used the next or previous radiobutton will be focused and checked.
The javascript is working fine in Mozilla, but when i try it in IE it fails. The keys 1-7 works in IE, but the arrowkeys dont work as expected.
Any suggestions?

Here is the javascript code:
<script language="javascript">
var thisfocus = -1;

function handlePress(e, totElements) {
var whichCode = (window.Event) ? e.which : e.keyCode;
if (String(whichCode)=="9"){
event.keyCode=0;
e.returnValue=false;
} else if(String(whichCode)=="49"){
if(0&gt;=totElements){
}else{
thisfocus = 0;
setFocus(thisfocus);
setChecked(thisfocus);
}
}else if (String(whichCode)=="50"){
if(1&gt;=totElements){
}else{
thisfocus = 1;
setFocus(thisfocus);
setChecked(thisfocus);
}
}else if (String(whichCode)=="51"){
if(2&gt;=totElements){
}else{
thisfocus = 2;
setFocus(thisfocus);
setChecked(thisfocus);
}
}else if (String(whichCode)=="52"){
if(3&gt;=totElements){
}else{
thisfocus = 3;
setFocus(thisfocus);
setChecked(thisfocus);
}
}else if (String(whichCode)=="53"){
if(4&gt;=totElements){
}else{
thisfocus = 4;
setFocus(thisfocus);
setChecked(thisfocus);
}
}else if (String(whichCode)=="54"){
if(5&gt;=totElements){
}else{
thisfocus = 5;
setFocus(thisfocus);
setChecked(thisfocus);
}
}else if (String(whichCode)=="55"){
if(6&gt;=totElements){
}else{
thisfocus = 6;
setFocus(thisfocus);
setChecked(thisfocus);
}
}else if (String(whichCode)=="56"){
if(7&gt;=totElements){
}else{
thisfocus = 7;
setFocus(thisfocus);
setChecked(thisfocus);
}
}else if (String(whichCode)=="57"){
if(8&gt;=totElements){
}else{
thisfocus = 8;
setFocus(thisfocus);
setChecked(thisfocus);
}

}else if (String(whichCode)=="97"){
if(0&gt;=totElements){
}else{
thisfocus = 0;
setFocus(thisfocus);
setChecked(thisfocus);
}
}else if (String(whichCode)=="98"){
if(1&gt;=totElements){
}else{
thisfocus = 1;
setFocus(thisfocus);
setChecked(thisfocus);
}
}else if (String(whichCode)=="99"){
if(2&gt;=totElements){

}else{
thisfocus = 2;
setFocus(thisfocus);
setChecked(thisfocus);
}
}else if (String(whichCode)=="100"){
if(3&gt;=totElements){

}else{
thisfocus = 3;
setFocus(thisfocus);
setChecked(thisfocus);
}
}else if (String(whichCode)=="101"){
if(4&gt;=totElements){

}else{
thisfocus = 4;
setFocus(thisfocus);
setChecked(thisfocus);
}
}else if (String(whichCode)=="102"){
if(5&gt;=totElements){

}else{
thisfocus = 5;
setFocus(thisfocus);
setChecked(thisfocus);
}
}else if (String(whichCode)=="103"){
if(6&gt;=totElements){

}else{
thisfocus = 6;
setFocus(thisfocus);
setChecked(thisfocus);
}
}else if (String(whichCode)=="104"){
if(7&gt;=totElements){

}else{
thisfocus = 7;
setFocus(thisfocus);
setChecked(thisfocus);
}
}else if (String(whichCode)=="105"){
if(8&gt;=totElements){

}else{
thisfocus = 8;
setFocus(thisfocus);
setChecked(thisfocus);
}
}else if (String(whichCode)=="37"){
alert(thisfocus);
thisfocus = thisfocus-1;
alert(thisfocus);
if(thisfocus&lt;0) thisfocus = 0;
setFocus(thisfocus);
setChecked(thisfocus);
}else if (String(whichCode)=="38"){
alert(thisfocus);
thisfocus = thisfocus-1;
alert(thisfocus);
if(thisfocus&lt;0) thisfocus = 0;
setFocus(thisfocus);
setChecked(thisfocus);

}else if (String(whichCode)=="39"){
alert(thisfocus);
thisfocus=thisfocus+1;
alert(thisfocus);
if(thisfocus&gt;totElements-1) thisfocus = totElements-1;
setFocus(thisfocus);
setChecked(thisfocus);

}else if (String(whichCode)=="40"){
alert(thisfocus);
thisfocus=thisfocus+1;
alert(thisfocus);
if(thisfocus&gt;totElements-1) thisfocus = totElements-1;
setFocus(thisfocus);
setChecked(thisfocus);

}


function setFocus(elementinst){
document.itemform.elements[elementinst].focus();
}
function setChecked(elementinst){
document.itemform.elements[elementinst].checked=true;
}
</script>

daheri
02-02-2006, 03:49 PM
Never mind. I found the error myself. Problem is I had not put cancelbuble and returnvalue false to the keys.