In this tutorail we will learn about how to stop the default action of an element from happening and for this purpose we will use jquery method event.preventDefault().
Example :-
Below example will show you how we can prevent a link to follow the URL. So when you click on the link it will not go to google.com rather it will give you an alert.
<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("a").click(function(event){
event.preventDefault();
alert("Hello World");
});
});
</script>
</head>
<body>
<a href="http://google.com/">Go to Google</a>
</body>
</html>
No comments:
Post a Comment
Please do not enter spam links.