Many times we need to use anchor tag in html coding. For anchor tag we assign href attribute with URL. So that whenever user click on that it will redirect to URL given as a value of href attribute. But how to prevent this default functionality?
We can prevent this default behavior of anchor tag using JavaScript or jQuery.
Simply, if we assign # to the href then after clicking on it will not redirect to any URL but it will jump to top of screen.
So, how to prevent that behavior?
- We can prevent default behavior of anchor tag by assigning javascript:void(0) in href then it will not redirect to any URL
Suppose anchor tag is defined as follows
By writing return false we can override default behavior of the element.
Suppose anchor tag is defined as follows
By writing event.preventDefault() we can override default behavior of the element.
Conclusion :
I hope that this article would have helped you in understanding to Prevent default behavior of anchor tag using JavaScript and jQuery. Your feedback and constructive contributions are always welcome.
No comments:
Post a Comment