1

Hey, yes, I know that with [funcname].caller or arguments.callee.caller you can get a reference to the function which called the actual function, BUT - the following scenario:

<a href="#" onclick="return something()">Test</a>

Inside of something() I have no chance to get to the A-Tag, even with the .caller reference, unless I alter the script in the following way:

<a href="#" onclick="return something(this)">Test</a>

With "this" I'm passing my A-Tag reference to the function but is there a way to get the A-Tag reference without explicitly passing it to the function?

2
  • what exactly you want to accomplish? If you want a link from javascript why not use window.navigate? Commented Feb 18, 2011 at 11:15
  • I want to perform an action after clicking on the A-Tag (or let it be an input:button) and alter the calling object. Lets say I want to change the Link/Button-Text to "Please wait". Commented Feb 18, 2011 at 11:16

1 Answer 1

0

Well, I think this is doing fine, it's just one word of code and it doesn't harm.Using jquery you can do this way

$(function(){
    $("body").delegate("a", "click", function(){
        alert($(this).html());
    });
});

//Both code will work for you

$(function(){
    $('a').click(
        function(){
            alert($(this).html());
        }
    );
})
Sign up to request clarification or add additional context in comments.

5 Comments

This way I would observe all A-Tags in my document, even if they don't call my somefunction() in an onclick event. And what if the user wants to use the function with an input:button? I don't think thats the right solution.
@Christian in that case use 'input[type=button]' instead of 'a', there are lot's of alternatives in jquery!!
@Christian besides you can filter a tags by putting classes $('a.classname') as well as by id $('a#id') or other various attributes $('a[name=somename]')
Yeah, sure - but this way I have to observe a million eventualities... Take look at my alternative idea in Alisey's post.
@Christian Engel Well have a look at stackoverflow.com/questions/5055228/… and webdevelopersjournal.com/articles/jsevents3/jsevents3.html and sure you will find jquery or passing this as an argument is more advisable. and about your alternative, sure you can do this better with passing this on function and integer as other parameter. But still i am not sure if i fully comprehend your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.