Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I delay the 'onchange' listener on a drop-down list?

Status
Not open for further replies.

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;
}
 
Thank you BillyRayPreachersSon.

But I found out that removing () out of the statement caused my function unable to work.

mySelect.onchange = moveFocus;

I'm still confused because as I have seen in other websites, your solution works well. But I use greasemonkey for Firefox to write my own script working on Wikipedia, and it does not work.
 
Perhaps it is because 'moveFocus' references 'mySelect' - yet 'mySelect' is not defined anywhere.

Perhaps you should use 'proper' ways of accessing DOM elements, not IE-only shortcuts. e.g. document.forms['formName'].elements['selectElName'] ?

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Ummm.. I may need some time to try the way you suggest. I am so new to javascript (about 6 days) so I do not know anything much. DOM is one thing I need to study more.

Anyway, thank you very much for your suggestion. I guess that it will definitely work, if I can use it in a right way. ^^
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top