views:

41

answers:

2

I'm writing a greasemonkey script that creates an auto-complete search type box which makes it easier to select from a large dropdown on a webpage. The dropdown has inline onchange code which I can't seem to get to trigger when I change the selection using javascript. Any ideas?

+1  A: 

Suppose the the page had something like:

<input onchange="someFunction()">

Then your Greasemonkey JavaScript could change the input value and then call the function using:

unsafeWindow.someFunction();
Brock Adams
Ah. That worked. Any ideas on how you'd stop the enter key from submitting the form?
Joren
@Joren: Open a new question for that and post details. In general you would change how you bind the event handler and/or use `event.preventDefault();`, `event.stopPropagation();`, and `return false;` in a/the appropriate event handler.
Brock Adams
A: 

You can trigger the onchange "really":

From within GM:

unsafeWindow.dropdownObject.onchange();

From the webpage:

dropdownObject.onchange();
Dr.Molle