w3resource

jQuery: Cancel the default action of the click

jQuery Fundamental - II : Exercise-92

Cancel the default action (navigation) of the click.

Sample solution :

HTML Code :

<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-git.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Cancel the default action (navigation) of the click.</title>
</head>
<body>
<a href="https://www.w3resource.com/">Clicked anchors will not take the browser to a new URL</a>
<p id="log"></p>
</body>
</html>

JavaScript Code :

$( "a" ).click(function( event ) {
event.preventDefault();
$( "<p>" )
.append( "Default " + event.type + " prevented" )
.appendTo( "#log" );
});

See the Pen jquery-fundamental-exercise-92 by w3resource (@w3resource) on CodePen.


Contribute your code and comments through Disqus.

Previous: Display the mouse position relative to the left and top edges of the document (within this iframe).
Next: Display previous handler's return value.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.