If the element has an onclick
attribute, do this simply by removing the onclick attribute. See the code below:
function myFunction() {
document.getElementById( 'element' ).removeAttribute( 'onclick' )
}
<a id="element" href="https://stackoverflow.com" onclick="alert( 'Stack Overflow' ); return false">Click here for alert.</a>
<p>Click the button below to remove event listener from above link.</p>
<button onclick="myFunction()">Remove Event Listener</button>
But, if the element does not have an onclick
attribute, you must have access to the click function and use removeEventListener()
Method. See the code snippet below:
document.getElementById( 'element' ).addEventListener( 'click', Zt )
function Zt( e ) {
e.preventDefault();
alert( 'Stack Overflow' )
}
document.getElementById( 'button' ).addEventListener( 'click', function() {
document.getElementById( 'element' ).removeEventListener( 'click', Zt )
} )
<a id="element" href="https://stackoverflow.com">Click here for alert.</a>
<p>Click the button below to remove event listener from above link.</p>
<button id="button">Remove Event Listener</button>