Recently on this newsgroup I asked for help with a questionnaire I was
doing in HTML & Javascript. I wanted a script that allowed me to use
the keys 1-9 to fill in the form rather than using the mouse (so where
there was a selection of radio controls, I would hit 1 to select the
first one, two for the second etc), and use Enter to move down the
form. Someone answered my post and provided a solution, but now I
need this script amending to do something else and it appears this
person is now unavailable. What I am requiring is the script
amending slightly so that as I am working my way down the questions,
the current question could be highlighted a different colour so I
could see easily which question I am on. All the questions are in
table cells so presumably this would make it easier. If I could
figure out what the writer of the script (pasted below) had done, I
could do this myself, but my knowledge of JS is rather limited.
Is anyone else able to help?
Steve Wylie
The script is:
<script type="text/javascript">
var snapForm;
var currentControl;
var currentGroup;
var nextControl
function active(thisElem ,nextElem) {
currentControl = thisElem;
nextControl = nextElem;
if (thisElem.name) {
currentGroup = thisElem.form.e lements[thisElem.name];
if (! currentGroup.le ngth) {
currentGroup = undefined;
}
}
}
function snapkey(event) {
event = event || window.event;
var key = event.keyCode || event.charCode || event.which;
if(key == 0x30 && currentGroup) { // 0
for (var i=0;i<currentGr oup.length;i++) {
currentGroup[i].checked = false;
}
return false;
}
if (0x31 <= key && key <= 0x39 && currentGroup) { // 1-9
var idx = key - 0x31;
if (idx < currentGroup.le ngth) {
currentGroup[idx].checked = true;
}
return false;
}
if (key == 13) {
if(nextControl) {
var next = nextControl;
next.focus();
next.onfocus();
return false;
}
}
return true;
}
function init(formName) {
var makeActiveCall = function(next) {
return function(){acti ve(this,next);} ;
};
var elems = document.forms[formName].elements;
var firstElem; // first named element. Is focused at start.
var currentName = "";
var currentIdx = 0;
for (var i=1;i<elems.len gth;i++) {
if (elems[i].type.toLowerCa se() != "hidden" &&
elems[i].name && currentName != elems[i].name) {
if (!firstElem) {firstElem = elems[i];}
for (var j = currentIdx; j < i; j++) {
if (elems[j].name) {
elems[j].onfocus = makeActiveCall( elems[i]);
}
}
currentIdx = i;
currentName = elems[i].name;
}
}
for (j=currentIdx;j <elems.length;j ++) {
elems[j].onfocus = makeActiveCall( );
}
firstElem.focus ();
firstElem.onfoc us();
document.onkeyp ress = snapkey;
}
</script>
doing in HTML & Javascript. I wanted a script that allowed me to use
the keys 1-9 to fill in the form rather than using the mouse (so where
there was a selection of radio controls, I would hit 1 to select the
first one, two for the second etc), and use Enter to move down the
form. Someone answered my post and provided a solution, but now I
need this script amending to do something else and it appears this
person is now unavailable. What I am requiring is the script
amending slightly so that as I am working my way down the questions,
the current question could be highlighted a different colour so I
could see easily which question I am on. All the questions are in
table cells so presumably this would make it easier. If I could
figure out what the writer of the script (pasted below) had done, I
could do this myself, but my knowledge of JS is rather limited.
Is anyone else able to help?
Steve Wylie
The script is:
<script type="text/javascript">
var snapForm;
var currentControl;
var currentGroup;
var nextControl
function active(thisElem ,nextElem) {
currentControl = thisElem;
nextControl = nextElem;
if (thisElem.name) {
currentGroup = thisElem.form.e lements[thisElem.name];
if (! currentGroup.le ngth) {
currentGroup = undefined;
}
}
}
function snapkey(event) {
event = event || window.event;
var key = event.keyCode || event.charCode || event.which;
if(key == 0x30 && currentGroup) { // 0
for (var i=0;i<currentGr oup.length;i++) {
currentGroup[i].checked = false;
}
return false;
}
if (0x31 <= key && key <= 0x39 && currentGroup) { // 1-9
var idx = key - 0x31;
if (idx < currentGroup.le ngth) {
currentGroup[idx].checked = true;
}
return false;
}
if (key == 13) {
if(nextControl) {
var next = nextControl;
next.focus();
next.onfocus();
return false;
}
}
return true;
}
function init(formName) {
var makeActiveCall = function(next) {
return function(){acti ve(this,next);} ;
};
var elems = document.forms[formName].elements;
var firstElem; // first named element. Is focused at start.
var currentName = "";
var currentIdx = 0;
for (var i=1;i<elems.len gth;i++) {
if (elems[i].type.toLowerCa se() != "hidden" &&
elems[i].name && currentName != elems[i].name) {
if (!firstElem) {firstElem = elems[i];}
for (var j = currentIdx; j < i; j++) {
if (elems[j].name) {
elems[j].onfocus = makeActiveCall( elems[i]);
}
}
currentIdx = i;
currentName = elems[i].name;
}
}
for (j=currentIdx;j <elems.length;j ++) {
elems[j].onfocus = makeActiveCall( );
}
firstElem.focus ();
firstElem.onfoc us();
document.onkeyp ress = snapkey;
}
</script>
Comment