how to get the currently linking element?

S

sowencheung

is it possible to get the currently linking element?

for example, I wire up an onclick event for some anchor tags, is it
possible in this onclick event, I can know which anchor tag is being
clicked?

"this" does not work

thanks!
 
R

Randy Webb

(e-mail address removed) said the following on 6/12/2006 11:15 AM:
is it possible to get the currently linking element?
Yes.

for example, I wire up an onclick event for some anchor tags, is it
possible in this onclick event, I can know which anchor tag is being
clicked?
onclick="alert(this)"

"this" does not work

Yes it does. Or post a sample that doesn't do what you think it should.
 
S

sowencheung

thanks for reply.

what I am doing is

// a is a link object
AddEventHandler(a, "click", specialExecution);

function specialExecution()
{
// if I alert(this); it shows [object]

// but if I do the following
var url = this.href;

// it is undefined
alert(url);
}
 
M

Marc

how about:


function addEvent(obj, type, fn){
if (obj.attachEvent){
obj['e'+type+fn] = fn;
obj[type+fn] = function(){obj['e'+type+fn](window.event);}
obj.attachEvent('on'+type, obj[type+fn]);
}else
obj.addEventListener(type, fn, false);
}

var a = document.getElementById("myaID");
addEvent(a, "click", function(){alert(this.href)});



BTW, the above code works in FireFox, but not in IE


thanks for reply.

what I am doing is

// a is a link object
AddEventHandler(a, "click", specialExecution);

function specialExecution()
{
// if I alert(this); it shows [object]

// but if I do the following
var url = this.href;

// it is undefined
alert(url);
}
 
R

Randy Webb

(e-mail address removed) said the following on 6/12/2006 11:28 AM:
thanks for reply.

Thanks for quoting what you are replying to next time.

If you want to post a followup via groups.google.com, don't use the
"Reply" link at the bottom of the article. Click on "show options" at
the top of the article, then click on the "Reply" at the bottom of the
article headers.

what I am doing is

// a is a link object
AddEventHandler(a, "click", specialExecution);

function specialExecution()
{
// if I alert(this); it shows [object]

That is because 'this' refers to the the current function which is an
[object], you don't need a reference to the function, you want a
reference to the link and the link will have to give the 'this' reference.
// but if I do the following
var url = this.href;

// it is undefined

That is because the function doesn't have an href property although you
could give it one. And since it doesn't exist, it is undefined.
alert(url);
}

Is AddEventHandler from Prototype.js?
 
L

Lasse Reichstein Nielsen

Randy Webb said:
// a is a link object
AddEventHandler(a, "click", specialExecution);
function specialExecution()
{
// if I alert(this); it shows [object]

That is because 'this' refers to the the current function which is an
[object], you don't need a reference to the function, you want a
reference to the link and the link will have to give the 'this'
reference.

No, in this case (invocation of an event handler attached using
attachEvent) "this" refers to the global object.

Had it been attached using addEventHandler, the "this" operator would
have provided the object that the handler had been added to.

/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top