By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,934 Members | 1,476 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,934 IT Pros & Developers. It's quick & easy.

attachEvent question

100+
P: 219
I'm trying to get attachEvent to work for IE, but am having a problem. It calls hello() the first time, but then the script errors out. Should hello() be returning something to the attachEvent function? I'm just testing that I can call a function from the attachEvent.

Expand|Select|Wrap|Line Numbers
  1. function hello(){
  2.     alert("Hello Friends");
  3.  
  4. }
  5. function load() {
  6.     var inputs = document.getElementsByTagName("input");
  7.     for (var i = 0; i < inputs.length; i++) {
  8.         alert(inputs[i].name);
  9.         //inputs[i].addEventListener ("onclick",hello(),false);
  10.         if (inputs[i].addEventListener){ // W3C DOM
  11.             alert(inputs[i] + "EventListener");
  12.             inputs[i].addEventListener("click",hello(),false);
  13.         }
  14.         else if (inputs[i].attachEvent) { // IE DOM
  15.             alert(inputs[i] + "attachEvent");
  16.             var r = inputs[i].attachEvent("on"+"click", hello());
  17.         }
  18.     } 
  19. }
  20.  
Oct 17 '07 #1
Share this Question
Share on Google+
17 Replies


100+
P: 428
do not run the function in the third argument, just reference it-
pass hello instead of hello()
Oct 17 '07 #2

100+
P: 219
Excellent, that worked like a charm. I knew it was something stupid I was doing it!!
Oct 17 '07 #3

100+
P: 219
Ok that worked, but what I'm trying to do is setup an event listener so that when a user clicks on a form field, if it contains the default value, it should clear it out.
I'm not sure how to access the value in the removeDef() function. Do I use this? I need to check


Here's what I have now.

Expand|Select|Wrap|Line Numbers
  1. function removeDef(){
  2.     //not sure what to put in this function.  This function should clear out default values when a user clicks on it.
  3.         //if(this.defaultValue==this.value) 
  4.                 //this.value = '';
  5. }
  6. function loadSecVars() {
  7.     var inputs = document.getElementsByTagName("input");
  8.     for (var i = 0; i < inputs.length; i++) {
  9.         if (inputs[i].addEventListener) // W3C DOM
  10.             inputs[i].addEventListener("click",removeDef,false);
  11.         else if (inputs[i].attachEvent) // IE DOM
  12.             inputs[i].attachEvent("on"+"click", removeDef);
  13.     } 
  14. }
  15.  
Oct 17 '07 #4

gits
Expert Mod 5K+
P: 5,390
hi ...

no ... you cannot use this ... you have to use the event itself. have a look at the following example:

Expand|Select|Wrap|Line Numbers
  1. function removeDef(e) {
  2.     alert(e.target.value);
  3. }
that is working for FF/Safari/Opera and IE, as you certainly expected, uses another way ... IE has a global event-object:

Expand|Select|Wrap|Line Numbers
  1. window.event.srcElement.value
that you might use.

kind regards
Oct 17 '07 #5

100+
P: 219
Ok I tried what you said but it's erroring when e.target.value is referenced. I see that alert(e) produces [object] so that's good, I'm just not referencing it correctly now. I'm obviously not understanding what you're saying. Sorry, I'm pretty much just jumping into javascript and don't have my experience with it. I'm definitely getting on board very late.

Expand|Select|Wrap|Line Numbers
  1. function removeDef(e){
  2.     //not sure what to put here
  3.     alert(e);
  4.     alert(e.target.value);
  5.     alert(window.event.srcElement.value);
  6. }
  7. function loadSecVars() {
  8.     var inputs = document.getElementsByTagName("input");
  9.     for (var i = 0; i < inputs.length; i++) {
  10.         if (inputs[i].addEventListener) // W3C DOM
  11.             inputs[i].addEventListener("click",removeDef,false);
  12.         else if (inputs[i].attachEvent) // IE DOM
  13.             inputs[i].attachEvent("on"+"click", removeDef);
  14.     } 
  15. }
  16.  
Oct 17 '07 #6

100+
P: 428
function removeDef(e){
//figure out which event model here:
var who= (window.event)? event.srcElement: e.target;
alert(who.value)
}
Oct 17 '07 #7

100+
P: 219
Thanks I got it working!

Expand|Select|Wrap|Line Numbers
  1. function clearField(which){
  2.     if(which.defaultValue==which.value) 
  3.         which.value = '';
  4. }
  5. function removeDef(e){
  6.     clearField(this);
  7. }
  8. function loadSecVars() {
  9.     var inputs = document.getElementsByTagName("input");
  10.     for (var i = 0; i < inputs.length; i++) {
  11.         if (inputs[i].addEventListener) // W3C DOM
  12.             inputs[i].addEventListener("click",removeDef,false);
  13.         else if (inputs[i].attachEvent) // IE DOM
  14.             inputs[i].attachEvent("on"+"click", removeDef);
  15.     } 
  16. }
  17.  
Using Firebug for firefox is such a huge help in troubleshooting these javascript issues. I think I'll have a much easier time now.
Oct 17 '07 #8

100+
P: 219
Oops, maybe I didn't get it working. I have it working in firefox, but not in IE. I'll try gits suggestion.
Oct 17 '07 #9

100+
P: 219
I tried window.event, but I only get [object] when I put it in an alert. How do I retrieve the value?

Geez I hate IE even more!!!
Oct 18 '07 #10

gits
Expert Mod 5K+
P: 5,390
try

Expand|Select|Wrap|Line Numbers
  1. window.event.srcElement.value
kind regards
Oct 18 '07 #11

acoder
Expert Mod 15k+
P: 16,027
Also, check out A Guide to Coding Cross-browser Scripts (the events-related articles).
Oct 18 '07 #12

100+
P: 219
try

Expand|Select|Wrap|Line Numbers
  1. window.event.srcElement.value
kind regards
Is that exactly what I type?
Oct 18 '07 #13

gits
Expert Mod 5K+
P: 5,390
for IE ... that should work ...
Oct 18 '07 #14

100+
P: 219
Excellent that worked!!

Question for ya.

How would I got about finding out the values of those elements?
Meaning: window , event, srcElement

I want to know what window, event, srcElement represent.
Oct 19 '07 #15

gits
Expert Mod 5K+
P: 5,390
hi ...

could you explain in more detail please? window represents the current window, srcElement represents the event.target in IE ... which means the node where the event fired, and event represents the event that invokes the corresponding handler ... click, change etc.

kind regards
Oct 19 '07 #16

100+
P: 219
hi ...

could you explain in more detail please? window represents the current window, srcElement represents the event.target in IE ... which means the node where the event fired, and event represents the event that invokes the corresponding handler ... click, change etc.

kind regards
You just answered my question. I was just wondering what those stood for so I know what I'm targeting using that line.
Oct 19 '07 #17

gits
Expert Mod 5K+
P: 5,390
hi ...

glad to hear that ;) ... post back in case you have more questions ...

kind regards
Oct 19 '07 #18

Post your reply

Sign in to post your reply or Sign up for a free account.