OnChange

in Script development
Subscribe to OnChange 4 posts, 3 voices



MonkeyMargie Scriptwright
ieWindows

Can I get some code that will do something whenever a field on my form is changed? Since the Grease Monkey script runs when the page is loaded, how can I get it to work after the fact?

 
mike cupcake Scriptwright
FirefoxMacintosh

I believe you can create an Event Listener that will run when the field is changed, or perhaps more usefully once it loses focus (the user clicks outside the field)

 
MonkeyMargie Scriptwright
ieWindows

I've tried this:
var elmLink = document.getElementById('MyFieldName);
elmLink.addEventListener("click", sayhello('world'), true);
but it seems to execute only when the page is loaded, not when I click on the field. I've tried other actions such as 'mousemove', 'click', etc., but nothing works. Help!

 
kuzu Scriptwright
FirefoxMacintosh

We need to distinguish here between referring to a function and calling a function. Example:

var aFunction = sayhello; // referring to; notice the lack of ()
var theResult = sayhello('world'); // calling

Assuming theResult is not a function, passing it to addEventListener will not accomplish much. Instead, it looks like you need to create another function which calls sayhello with the correct parameters, and pass that new function:

function aFunction () {
  sayhello('world');
}
var elmLink = document.getElementById('MyFieldName');
elmLink.addEventListener("click", aFunction, true);

Alternatively, you can use an anonymous function:

var elmLink = document.getElementById('MyFieldName');
elmLink.addEventListener("click", function () { sayhello('world'); }, true);