views:

104

answers:

8

How do I get a reference to the clicked link to delete the correct row?

<tr>
<td>c1r1</td>
<td>c2r1</td>
<td><a href="javascript:delete_row();">delete</a></td>
</tr>

<tr>
<td>c1r2</td>
<td>c2r2</td>
<td><a href="javascript:delete_row();">delete</a></td>
</tr>

function delete_row() {
    this.parent().parent().remove();
}

I know I can use (in jquery)

$('a').click(function() {
    this.parent().parent().remove();
}

Or even this

$('a').live('click', function() {
    this.parent().parent().remove();
});

To bind the function to dynamically created links.

But I'm looking for the way to get a reference to the clicked link without jquery. I'm using jquery inside the function, but that is not the point.

Edit Many are suggesting to use this in the function as parameter, I have tried that, but it returns window:

<a href="javascript:delete_row(this);">delete</a>

function delete_row(elem) {
    console.log(elem);
}

Firebug console: Window config_maker.php
+2  A: 

You can pass the reference using this.

<a href="javascript:delete_row(this);">
Teja Kantamneni
I did try that, but it still returns `window`
Majid
It should not, post your `delete_row` code.
Teja Kantamneni
Try using onclick, where I'm pretty sure this will point to the link.
Bart van Heukelom
@Majid Yep, `this` does refer to `window` in this case. See my answer for an explanation and a solution.
Josh Stodola
A: 

Use this

<a href="javascript:delete_row(this);">
Júlio Santos
A: 

Can't you just use this as parameter of the function?

<a href="javascript:delete_row(this);">xxx</a>

And in Javascript:

function delete_row(clickedLink) {
    ...
}
romaintaz
A: 

Use...

function delete_row(link) {
    link.parent().parent().remove();
}

Then call javascript:delete_row(this);

Simon Lee
+10  A: 

Contrary to all the other answers, you cannot pass this in this case, because that would be referring to the window object and not the link. Why? Because you are not using an event handler. You are using the javascript: protocol. Don't use that to invoke your functions, but use an event handler instead. Change your links to this and you'll be straight...

<a href="javascript:void(0);" onclick="delete_row(this);">delete</a>

This is still far from ideal, as unobtrusive Javascript is the way to go these days. But this will at least get your code working.

Josh Stodola
+1 for the last (unobtrusive) comment which I truly wish would become an axiom in javascript context.
Mark Schultheiss
@Mark Yeah trust me, I cringed as I typed out `javascript:void(0);` It's been a long time since I've done that!
Josh Stodola
Thanks! Also, I remember I saw a tutorial in the early days of jquery that messed with the `target` and somehow discovered the `a`, but I have lost it and can't find it anymore.
Majid
@Majid Yes that is possible as well, using `event.target`, but even then you will run into cross-browser issues because IE calls it `srcElement` and not target!
Josh Stodola
How do you handle the href field in unobtrusive javascript? I always use `javascript:void(0);` because `#` has a tendency to take you to the top of the page.
Andrew Koester
@Andrew Depends. What I generally do is `<a id="whatever" href="http://enable-javascript.com/">` and then make sure to cancel the click event (by returning false in the handler or calling `preventDefault()`). This is nice because people with Javascript disabled will be elegantly redirected to a helpful resource. You can also use `#`, and cancel the click event to prevent it from taking you to the top of the page.
Josh Stodola
@Josh Ah-hah, I forgot about canceling events. Thanks.
Andrew Koester
A: 
<tr id='row1'><td><a rel="row1" href="javascript:delete_row('row1');">delete</a></td></tr>

<tr id='row2'><td><a rel="row2" href="javascript:delete_row(this.rel);">delete</a></td></tr>

...

function delete_row(varID) {
document.getElementById(varID).remove();

}

or

function delete_row(varID) {
$('#'+varID).remove();

}

FatherStorm
A: 

just check this object inside handler function

use : this.id

mahmoud
A: 

this might help You

document.addEventListener("click", listener);
function listener(event){
     if(event.target.nodeType != 'a') return false;
     document.remove(event.target.parentNode.parentNode);
}
Shusl