[thelist] JavaScript onChange question

Jeff C jeffc33 at hotmail.com
Tue May 7 13:52:12 CDT 2002

>Currently, the drop downs are only tied to the Textarea of the form (by
>name).  The forms that I am putting together would actually have up to 5
>input areas that would need a special character. Is it possible to use
>onChange() to replace the string based on Cursor location instead of a
>specific Textarea?

Assuming that a person would put focus into the text area or text input thay
wanted to add the special character to, here is one way to approach this

Declare a global variable in your script to track the name of the last text
area or text input that had focus.  (focus would be lost when they use the
drop downs, so you want to track what the last thing to have focus before

for example
var daFocus = 0

Add an event handler to each text area or text input:
onfocus = "trackFocus(this.name)"

add a function that will keep track of what had focus:
function trackFocus(daName)
	var daLength = document.textform.length;
	for (i=0;i<daLength;i++)
			if (document.textform.elements[i].name == daName)
			{daFocus = i}

Then modify the function you're calling with your onchange to use the

function handleListChange(theList)
	var numSelected = theList.selectedIndex ;
	if (numSelected != 0)
		textValue = document.textform.elements[daFocus].value ;
		textValue = textValue + theList.options[numSelected].value ;
		document.textform.elements[daFocus].value = textValue ;
		theList.selectedIndex = 0 ;
		document.textform.elements[daFocus].focus() ;

That worked in a quick test I set up on my development server.



Join the world’s largest e-mail service with MSN Hotmail.

More information about the thelist mailing list