jQuery event.preventDefault()

Introduction

Prevent a link from opening the URL:

View in separate window

<!DOCTYPE html>
<html>
<head>
<script 
 src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>/*from  ww w  . j a v a 2 s. c  om*/
<script>
$(document).ready(function(){
  $("a").click(function(event){
    event.preventDefault();
  });
});
</script>
</head>
<body>

<a href="http://java2s.com/">Go to java2s.com</a>

<p>The preventDefault() method will prevent the 
link above from following the URL.</p>

</body>
</html>

The event.preventDefault() method stops the default action of an element.

event.preventDefault()
Parameter Optional Description
event Required. The event parameter comes from the event binding function



PreviousNext

Related