T
tismee
hello,
there is a useful script at http://www.anvandbart.se/accessible-javascript-onchange-select-menu
which does subj.
unforunately, it only looks for <select>s with a hardcoded default
<option> ("select-one").
how do I modify it to make it more generic, i.e. something like with
getElementsByTagName("select") for instance?
PS
looking for a particular form.getElementById("select_id") is not
desirable (if avoidable by another method) as well, since it is Not
known (to me) which 'Id' will be assigned to any dropdown.
---
niceSelect=function(){
var f=document.forms;
for (var i=0;i<f.length;i++){ // Walks all the forms in
the document.
var e=f.elements;
for(var j=0;j<e.length;j++){ // Walks all the elements in
the form.
if(e[j].type=="select-one"){ // Chooses elements that are
select
// box (that does not allow multiple
// selections).
e[j].formnu=i;
// Here comes the "trick" of this
// script: it redefines the
// event handlers of the element.
e[j].onclick=function(){ // If the selection is made
// with the mouse, the it
// behaves like a normal selectbox
// menu and submits).
this.onchange=function(){
f[this.formnu].submit()
}
};
e[j].onblur=function(){ // This "disarms" the onblur
function
// so that it will not submit as soon
// as the default option is deselected
// (by moving down the select list with
// the arrow key).
this.onchange=function(){return true}
};
e[j].onkeydown=function(e){ // When keys are pressed on
the keyboard...
if (e){theEvent = e} else {theEvent=event};
if (theEvent.keyCode==13){ // ...only submit when 'enter'
is pressed.
if((this.onchange+"").indexOf("submit")<0){
f[this.formnu].submit()
}
}
}
}
}
}
}
there is a useful script at http://www.anvandbart.se/accessible-javascript-onchange-select-menu
which does subj.
unforunately, it only looks for <select>s with a hardcoded default
<option> ("select-one").
how do I modify it to make it more generic, i.e. something like with
getElementsByTagName("select") for instance?
PS
looking for a particular form.getElementById("select_id") is not
desirable (if avoidable by another method) as well, since it is Not
known (to me) which 'Id' will be assigned to any dropdown.
---
niceSelect=function(){
var f=document.forms;
for (var i=0;i<f.length;i++){ // Walks all the forms in
the document.
var e=f.elements;
for(var j=0;j<e.length;j++){ // Walks all the elements in
the form.
if(e[j].type=="select-one"){ // Chooses elements that are
select
// box (that does not allow multiple
// selections).
e[j].formnu=i;
// Here comes the "trick" of this
// script: it redefines the
// event handlers of the element.
e[j].onclick=function(){ // If the selection is made
// with the mouse, the it
// behaves like a normal selectbox
// menu and submits).
this.onchange=function(){
f[this.formnu].submit()
}
};
e[j].onblur=function(){ // This "disarms" the onblur
function
// so that it will not submit as soon
// as the default option is deselected
// (by moving down the select list with
// the arrow key).
this.onchange=function(){return true}
};
e[j].onkeydown=function(e){ // When keys are pressed on
the keyboard...
if (e){theEvent = e} else {theEvent=event};
if (theEvent.keyCode==13){ // ...only submit when 'enter'
is pressed.
if((this.onchange+"").indexOf("submit")<0){
f[this.formnu].submit()
}
}
}
}
}
}
}