unigodx
Programmer
- Jan 1, 2009
- 8
Can anyone please kindly answer my javascript question?
I write a script that uses a library to add new shortcuts to a web page.
One shortcut's function is, when pressed, a drop-down list will be added to a page.
Inside that drop-down list, there are lists of location within that page to jump to.
My intention is: When I press my shortcut, a drop-down list will appear.
The first element is the list must be automatically focused.
After I use arrow buttons to move up and down for selecting the right element, I will press Enter
and the page will move to a location referred from the selected element.
My problem is: When I pressed my shortcut, a drop-down list appeared as I wanted.
However, although I did not do anything after pressing the shortcut,
it immediately moved to a location referred from the first element in a list.
I suspect that I may misunderstand the way the codes work, especially 'onchange' listener.
Can you please help me solve my problem? I attached some of my codes below.
... //code create many elements and some libraries
shortcut.add("Ctrl+Shift+1", function() {
createIndexForm();
}
//create a drop-down list on a page
//myLocation is where a list will appear on a page
//I have already done mySelect.appendChild(myOptions)
function createIndexForm(){
myForm.appendChild(mySelect);
myLocation.appendChild(myForm);
mySelect.focus();
mySelect.onchange = moveFocus();
}
function moveFocus(){
window.location.href = mySelect.options[mySelect.selectedIndex].value;
}
I write a script that uses a library to add new shortcuts to a web page.
One shortcut's function is, when pressed, a drop-down list will be added to a page.
Inside that drop-down list, there are lists of location within that page to jump to.
My intention is: When I press my shortcut, a drop-down list will appear.
The first element is the list must be automatically focused.
After I use arrow buttons to move up and down for selecting the right element, I will press Enter
and the page will move to a location referred from the selected element.
My problem is: When I pressed my shortcut, a drop-down list appeared as I wanted.
However, although I did not do anything after pressing the shortcut,
it immediately moved to a location referred from the first element in a list.
I suspect that I may misunderstand the way the codes work, especially 'onchange' listener.
Can you please help me solve my problem? I attached some of my codes below.
... //code create many elements and some libraries
shortcut.add("Ctrl+Shift+1", function() {
createIndexForm();
}
//create a drop-down list on a page
//myLocation is where a list will appear on a page
//I have already done mySelect.appendChild(myOptions)
function createIndexForm(){
myForm.appendChild(mySelect);
myLocation.appendChild(myForm);
mySelect.focus();
mySelect.onchange = moveFocus();
}
function moveFocus(){
window.location.href = mySelect.options[mySelect.selectedIndex].value;
}