How do I know which link was clicked?

How do I get a link to link to link to remove the correct line?

<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 what I can use (in jQuery)

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

Or even that

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

To bind a function to dynamically created links.

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

Edit Many suggest using thisfunction as a parameter, I tried this, 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
+3
source share
8 answers

, this , window, . ? . javascript:. , . , ...

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

, Javascript - . , , .

+11

, this.

<a href="javascript:delete_row(this);">
+1
<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();

}

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

}

0

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

this

<a href="javascript:delete_row(this);">
-1

this ?

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

Javascript:

function delete_row(clickedLink) {
    ...
}
-1
source

Using...

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

Then call javascript:delete_row(this);

-1
source

just check the thisobject inside the handler function

use: this.id

-1
source

Source: https://habr.com/ru/post/1769150/


All Articles