user1844626
user1844626

Reputation: 1868

Detecting a HTML link is clicked or not

I have a table of links and there is no 'id' or 'name' on any tag. I want to check by simple javascript that which link('a' tag which called the function 'check') was clicked because I'll change the inner html of that 'a' tag then. I don't want to touch php or any language else but javascript. Is it possible to check this without using any id or name ?

My HTML:

<script type="text/javascript">
    function check(){
        var row=document.getElementsByTagName('tr');
        //row check
        for(var i=1;i<=row.length;i++){
            //column check
            var grand = row[i].getElementsByTagName('td');
            for(var j=0;j<=grand.length;j++){
                if(j==1){
                    var parent = grand[1].getElementsByTagName('a');
                    //    alert(parent.click);
                    //check here if the link was clicked
                }
            }
        }
    }
</script>
<table>
    <tr>
        <td>east</td>
        <td><a href="http://www.east.com" onclick="check();">www.east.com</a></td>
        <td>...</td>
    </tr>
    <tr>
        <td>west</td>
        <td><a href="http://www.east.com" onclick="check();">www.east.com</a></td>
        <td>...</td>
    </tr>
    <tr>
        <td>north</td>
        <td><a href="http://www.east.com" onclick="check();">www.east.com</a></td>
        <td>...</td>
    </tr>
    <tr>
        <td>south</td>
        <td><a href="http://www.east.com" onclick="check();">www.east.com</a></td>
        <td>...</td>
    </tr>
</table>

Upvotes: 0

Views: 465

Answers (1)

T J
T J

Reputation: 43156

You can pass the reference to clicked link as an argument using this as follows:

<a href="http://www.east.com" onclick="check(this);">www.east.com</a>

And JS:

function check(elm){ // elm now refers to clicked link.
  elm.href // www.east.com clicked, you can assign something else now.
}

Upvotes: 3

Related Questions