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

attachEvent - wish to provide arguments to calling method

P: n/a
I wish to add an event to an element, but I wish to add arguments to
the calling method, eg like so:

element.attachEvent('onclick',startDragDrop('arg1' ,arg2'))

I understand this is not possible. For a non windows browser I have
been successful with the following code:
newA.data = "argumentText"
if(navigator.appName=="Microsoft Internet Explorer"){
//attach events
}
else{
newA.onmouseover = function handler(evt){imageOn(this.data);}
newA.onmouseout = function handler(evt){imageOff(this.data);}
}

by way of a handler, but how could I do this with the IE? the same
code won't work

Feb 4 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Feb 5, 7:27 am, "Rupe" <rup...@web-ideas.com.auwrote:
I wish to add an event to an element, but I wish to add arguments to
the calling method, eg like so:

element.attachEvent('onclick',startDragDrop('arg1' ,arg2'))

I understand this is not possible. For a non windows browser I have
been successful with the following code:

newA.data = "argumentText"
if(navigator.appName=="Microsoft Internet Explorer"){
//attach events
}
else{
newA.onmouseover = function handler(evt){imageOn(this.data);}
newA.onmouseout = function handler(evt){imageOff(this.data);}
}

by way of a handler, but how could I do this with the IE? the same
code won't work
You will find this thread helpful (subject " attachEvent and
arguments"):

<URL: http://groups.google.com.au/group/comp.lang.javascript/
browse_frm/thread/92187c8705eb3e79/903e4f22ce34512d?lnk=gst&q=
+attachEvent+and+arguments&rnum=1#903e4f22ce34512d >
--
Rob

Feb 4 '07 #2

P: n/a
my solution actually got it to work by using an intermediary function
and the event object. But I don't think it's ideal

function myEvent(evt){
if(evt.type=="mouseover")
imageOn(evt.srcElement.id);
else
imageOff(evt.srcElement.id);
}

function imageOn(imgName) {
if (document.images) {
if(isIE){
document.all[imgName].src = onImgArray[imgName].src;
}
else
document.images[imgName].src = onImgArray[imgName].src;
}
}

two functions to run what should simply be one, is pretty ordinary,
but it got the job done.

Feb 5 '07 #3

P: n/a
Rupe wrote:
my solution actually got it to work by using an intermediary function
and the event object. But I don't think it's ideal
Far from it.
>
function myEvent(evt){
if(evt.type=="mouseover")
imageOn(evt.srcElement.id);
else
imageOff(evt.srcElement.id);
}

function imageOn(imgName) {
if (document.images) {
if(isIE){
Browser detection went out of fashion a long time ago, use feature
detection or a different scheme to link the elements.

document.all[imgName].src = onImgArray[imgName].src;
}
else
document.images[imgName].src = onImgArray[imgName].src;
There appear to be some braces missing here.

}
}

two functions to run what should simply be one, is pretty ordinary,
but it got the job done.
It seems you have either an element with an ID that is the same as some
other element's name property or you are using an element reference to
get its id to get a reference back to the same element. Both are poor
design decisions - the first is worse than the second.

You should be aware that evt.srcElement will not work in browsers that
do not support IE's proprietary event model - the W3C equivalent is
event.target.

Also, the element referenced by event.target may be a text node and
therefore may not have an ID attribute, or at least not the one you are
after.

If you explain a bit more about what you are trying to do, you'll get
better help.
--
Rob
Feb 5 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.