views:

135

answers:

4

I have elements being generated by dynamic html, I would like to reference the particular href that is calling the function when one of many may be calling it.

<a href="javascript:Foo(this)">Link</a>

Does not work when I try to reference $(this). Is there another way to do this or do I have to make dynamic ids?

+4  A: 

<a href="javascript:Foo(this.href)">Link</a> will work if you want to pass the href.

However, onsomething handlers in the html code are not jqueryish at all. Give your links a class and setup a live handler:

$('.mylink').live('click', function() {
    // do whatever you want with this or $(this) or this.href or $(this).attr('href')
});
ThiefMaster
Why bring jQuery into an equation where it is absolutely not needed? This is much easier to do, and easier to understand just using vanilla javascript.
Sean Kinsey
@Sean Kinsey ask the OP... the OP tags jQuery, what do you expect?... :p
Reigel
I expect the OP to not know better (than to assume that you need jQuery for this), and I expect those who answer to do know better.
Sean Kinsey
@Sean - Please read before encouraging outdated practices: http://en.wikipedia.org/wiki/Unobtrusive_JavaScript
Nick Craver
@Nick I know exactly what unobtrusive javascript is, and I'm telling you, it has no value as long as the fallback (no js) cannot replicate the intended behavior. So please add valid arguments instead of just pointing to a generalized wikipedia article
Sean Kinsey
@Sean - Since when would no js allow her function to run? None of this works without javascript...that's a completely invalid argument.
Nick Craver
Exactly :) And hence any reference to unobtrusive javascript is invalid as the premises for calling anything unobtrusive is that it _enhances_ the default behavior instead of _defining_ it.
Sean Kinsey
`<a href="javascript:Foo(this.href)">Link</a>` does not work.
lincolnk
A: 

Well, when using jQuery you don't even have to create id's. Just use jQuerys selector engine to grab those anchors.

jAndy
Why bring jQuery into an equation where it is absolutely not needed?
Sean Kinsey
Why not reading tags before creating comments?
jAndy
I did. It being tagged with jQuery is still no reason to over-complicate what could easier be done without.
Sean Kinsey
easier? maybe, way out of date and no obtrusive javascript? for sure.
jAndy
Are you serious? Good luck with that attitude, you clearly have no real understanding of the language (or you would have known that neither of the solutions are non-obtrusive as long as the anchor doesn't have a valid HREF that can do the exact same operation).
Sean Kinsey
@Sean - Inline script handlers, especially repeated hundreds or thousands of times is very out of date, harder to maintain, harder to optimize, more payload for the client to download **every page**...I could go on?
Nick Craver
@Sean: same question goes back to you, are YOU serious ?
jAndy
@Nick, the premises (if you read the question) is that this code is generated, so your argument is invalid. About the payload? Sure.@jAndy, oh yes.
Sean Kinsey
The point here is that there is nothing to suggest that jQuery is required to be present (except for the jQuery tag, which means nothing in here as most think that javascript is built on top of jQuery). So the pragmatic approach is to solve without, especially when that actually results in more readable and smaller code, removes the requirement of a bloated library, lets you switch libraries without affecting the code ... I could go on..
Sean Kinsey
@Sean - The poster is trying to use `$(this)` they are using jSuery. Since the user is already using jQuery, having inline script is more bloated than a single external handler...your argument is backwards and invalid here since they're already including the library.
Nick Craver
@Sean: I'm not going down on your level offending people, be aware that I've got a pretty deep understanding of js. Nick already mentioned what is the point here, you are just wrong / outdated.
jAndy
I'm not trying to offend anyone, but as someone who also has quite a deep understanding of js, I prefer to choose the pragmatic solution. Even with jQuery in place, I still prefer `function getEl(id){return document.getElementById(id);}` over the awful `jQuery/$` method when getting a reference to a DOM element. The only reason why I'm bothering to follow up on this discussion is that I'm sick and tired of newbies answering dead-simple questions with complex and unnecessary jQuery-based answers, leading the other newbie to believe that you cannot do even the simplest thing without it.
Sean Kinsey
@Sean - First, generated code provides even **more of reason** for using unobtrusive script...how is maintaining script in both your generation code/page template **and** the matching external file easier to maintain? It's not. No one said you couldn't do everything you wanted without any library, that's of course true because whatever library you're using *is* javascript. The point is that external unobtrusive javascript has **many advantages** and you leading the new users to believe the only way to do this is in-line is outdated and you're causing them more maintenance headaches later.
Nick Craver
A: 

No point in using the javascript: pseudo protocol handler.
Use

<a href="#" onclick="Foo(this);">Link</a>

instead.

Sean Kinsey
A: 

putting the js parts in the href attribute is a bad idea. best practice is adding a handler with addEventListener but here you can get away with setting onclick directly.

<a href="#" onclick="Foo(event);">Link</a>

and your function would be like

function Foo(e) {

    var a = e.target || e.srcElement;

    // TODO: stuff

    if (e.preventDefault) {
        e.preventDefault();
    }
    else {
        return false;
    }
}

so when you click the link, Foo is called with the event as a paremeter. the event object has a reference to the source element, either as target in standards browsers or srcElement in IE. the preventDefault/return false; combo at the end prevents the browser from "following" the link to #.

edit: on second thought, since you have many links, using jquery to add handlers the recommended way is probably better (although the first solution is still fine).

...
<a id="A5" href="#" >Link</a>
<a id="A6" href="#" >Link</a>
<a id="A7" href="#" >Link</a>
...

<script>
$('a').click(Foo);
</script>
lincolnk