Martin said:
I want to create a funtion that will be called from the onChange event
You say "called from", not "called as". I assume that this means that
your function isn't assigned directly to handle the event (because
in that case you get the input element as "this" value during execution
of the handler function - unless you use the IE specific attachEvent,
ofcourse).
of an input that will prompt the user if the entered value is out of
range. This function will be called from many different inputs.
Is there a way I can get the value (inside the function) without
having to explicitly pass it to the function.
I assume you mean the value of the "value" property of the input
element in question.
In that case ... neh.
Yes, you can avoid passing the value explicitly, if you pass it implicitly
by passing something else explicity which holds the value or a reference to
and object that can be used to get the value.
No, you can't avoid passing something explicitly that is used to get
the value.
In *some* browsers you can, because the event is globally available
as window.event. However, that's not part of the DOM Event specification,
and in other browsers, most notably Firefox, that won't work.
IOW, is there someway
that the function can know which input called it and extract that
input's current value? Maybe something like: "this.value" ?
If you do:
inputElement.addEventListener("change", myFunction, false);
to attach the function (or even the old-style
inputElement.onchange = myFunction;
but I recommend the former where available), then "this" will provide
the inputElement during execution of the function.
Old IE's doesn't have addEventListener, so there you'll either have
to use the ".onchange=" strategy, or do some wrapping of the function
before passing it to attachEvent.
If you call the function from an intrinsic event handler, i.e.,
<input ... onchange="myFunction();">
then no. There won't be a way that works everywhere, but in that case,
don't call it like that. Use:
<input ... onchange="myFunction.call(this);">
instead, to explicitly pass the input element as the "this" value,
or just
<input ... onchange="myFunction(this);">
to just pass it as an argument. It must be build to expect the value
anyway, so which way it expects to get it shouldn't be important.
Personally, I'd use addEventListener.
/L