View Full Version : Accessing form element properties using onchange?

07-05-2006, 05:29 PM

I have 1 form, and dozens of elements. In the select elements I use onchange="somefunc()". In this function I'd like to access and change the form element attributes (such as form.element.option[].value and form.element.option[].innerHTML).

Is there a way to access the element that has just changed (unsing onchange="somefunc()") by passing a "this.element" parameter to access the elements properties such as form.element.option[].value within the function?


07-05-2006, 06:26 PM
you can pass "this" and that will give you a reference to the select field.


Or you can use:

// ** code blatantly copied and pasted from www.quirksmode.org
var targ;
if (!e) var e = window.event;
if (e.target) targ = e.target;
else if (e.srcElement) targ = e.srcElement;
if (targ.nodeType == 3) // defeat Safari bug
targ = targ.parentNode;

putting this at the very beginning of your event handler will result in the variable targ pointing to the element that fired/dispatched the event.

Hope that helps.

07-05-2006, 06:46 PM
it sure does help! thanks a bunch man