Closed Thread Icon

Topic awaiting preservation: Cross Browser onchange() (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=26233" title="Pages that link to Topic awaiting preservation: Cross Browser onchange() (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Cross Browser onchange() <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-13-2005 16:38
code:
if (inputobj.hasAttribute("onchange")){
  inputobj.onchange();
}



Is there a way to make the above snippet work in both IE and FF?

In IE I first get an error with the first line, so I changed my code to

code:
if (inputobj.onchange){
  inputobj.onchange();
}



But then I get an error for inputobj.onchange().

Is there a reason for this?

Dan @ Code Town

Iron Wallaby
Paranoid (IV) Inmate

From: USA
Insane since: May 2004

posted posted 07-13-2005 16:50

I've had a similar problem -- it seems that MSIE does not allow you to call an event handler as a function (which is pretty stupid, but oh well -- I'll save the MS rant for another time).

There are a number of workarounds. A simple one being:

code:
functon fooHandler () {
   ... do stuff ...
}

inputobj.onchange = fooHandler;

... later ...

fooHandler(); // instead on inputobj.onchange();


For this, we simply have a helper function that is equivalent to the onClick event handler, and call that instead. You may want to do some mangling if you need the "this" object.

---
Website

WarMage
Maniac (V) Mad Scientist

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

posted posted 07-13-2005 16:55

Ok, that makes a bit of sense.

I need to handle a this. In particular I need to call the function valueChanged(this).

valueChanged() is in a completely separate library, which is not included in the library I am calling the onchange handler from.

I was thinking something like an eval?

Dan @ Code Town

WarMage
Maniac (V) Mad Scientist

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

posted posted 07-13-2005 16:58
code:
if(inputobj.onchange){
  var change = inputobj.onchange
  change = change.replace("this","inputobj");
  eval(change);
}



Maybe? Off to test this.

Dan @ Code Town

(Edited by WarMage on 07-13-2005 17:01)

WarMage
Maniac (V) Mad Scientist

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

posted posted 07-13-2005 17:16
code:
if (inputobj.hasAttribute){
  if(inputobj.hasAttribute("onchange")){
    inputobj.onchange();
  }
} else if (inputobj.onchange){
  var change = inputobj.onchange;
  change = change.replace("this","inputobj");
  eval(change);
}



This is the work around I have, and it works in both FF and IE for the tests that I have run.

Thanks for the help.

It is funny, I did some googling and all of the solutions for programatically firing the onchange was to call onchange() nothing mentioned that it didn't work in IE.

Dan @ Code Town

(Edited by WarMage on 07-13-2005 17:17)

« BackwardsOnwards »

Show Forum Drop Down Menu