Closed Thread Icon

Topic awaiting preservation: Setting onChange in IE via JS (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=26331" title="Pages that link to Topic awaiting preservation: Setting onChange in IE via JS (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Setting onChange in IE via JS <span class="small">(Page 1 of 1)</span>\

 
WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 07-26-2005 23:33

I am again having onChange issues.

I have a function

code:
function setGridOnChange(){
	var form = document.forms[0];
	var pattern = /(\w+\.\d+)\.(\w+)/
		for(i = 0; i < form.elements.length; i++){
			var input = form.elements[i]; 
			if(input.id.match(pattern) && pattern.exec(input.id)[2] != 'rowstate'){
				input.setAttribute("onchange","valueChanged(this)");		
			}
		}
}



The function walks elements that have a pattern and set the onchange attribute to the function valueChanged(this). It works fine in FF, but I just found out that this does not work in IE.

Is there any way to get this to work with IE?

Thanks,

Dan @ Code Town

COBOLdinosaur
Nervous Wreck (II) Inmate

From: Deep in a cave
Insane since: May 2005

posted posted 07-26-2005 23:50

You probably need to attach the even to get it to work in IE:

input.attachEvent('onchange',valueChanged);

Invent a better mouse trap and a new improved mouse will come along and steal the cheese.

Cd&

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 07-26-2005 23:55

I googled some more and found that! Thank you.

How is the 'this' handled. I am not getting this set correctly.

code:
function setGridOnChange(){
	var form = document.forms[0];
	var pattern = /(\w+\.\d+)\.(\w+)/
		for(i = 0; i < form.elements.length; i++){
			var input = form.elements[i]; 
			if(input.id.match(pattern) && pattern.exec(input.id)[2] != 'rowstate'){
				input.onchange = "valueChanged(this)";
				var onChangeHandler = new Function(input.onchange);
        if(input.addEventListener){
					input.addEventListener('change',onChangeHandler, false);
				} else if (input.attachEvent) {
         	input.attachEvent('onchange',onChangeHandler);
				}
			}
		}
}



Am I handling the "this" incorrectly? I guess I obviously am, but is there a better way for me to handle this?

Dan @ Code Town

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 07-27-2005 00:03

I did a bit of a work around. Instead of using 'this' I have passed in the input.id and then I am calling document.getElementById() on the supplied value.

If you have a workaround for the 'this' problem I would be interested.

Thanks again,

Dan @ Code Town

« BackwardsOnwards »

Show Forum Drop Down Menu