Skip to content Skip to sidebar Skip to footer

Disable OnClick Event If Anchor Href Link Was Executed

I have a table. Each row is a link to some page(for example - google.com) which called by js 'onClick window.open' method:

Solution 1:

I would recommend you to change approach a little. Inline event handlers are never good idea. Consider this code.

HTML:

<table id="table">
    <tr data-url="http://google.com">
        <td>Content</td>
        <td>Content</td>
        <td><a href="http://jsfiddle.net">JSFiddle</a></td>
    </tr>
    ...
</table>

JS:

var table = document.getElementById('table');

table.addEventListener('click', function(e) {

    var target = e.target;
    if (target.tagName == 'A') {
        return false;
    }

    if (target.tagName == 'TD') {
        var win = window.open(target.parentNode.getAttribute('data-url'));
        win.focus();
    }
}, false);

Demo: http://jsfiddle.net/CqRE9/


Solution 2:

Yes it's possible!

$("tr").on("click", function(){ 
 $(this).attr("onClick", false); 
});

Demo: fiddle


Post a Comment for "Disable OnClick Event If Anchor Href Link Was Executed"