preventDefault() on an <a> tag

JavascriptJquery

Javascript Problem Overview


I have some HTML and jQuery that slides a div up and down to show or hide` it when a link is clicked:

<ul class="product-info">
  <li>
    <a href="#">YOU CLICK THIS TO SHOW/HIDE</a>
    <div class="toggle">
      <p>CONTENT TO SHOW/HIDE</p>
    </div>
  </li>
</ul>

$('div.toggle').hide();
$('ul.product-info li a').click(function(event){
  $(this).next('div').slideToggle(200);
}

My question is: How do I use preventDefault() to stop the link acting as a link and adding "#" to the end of my URL & jumping to the top of the page?

I can't figure out the right syntax, I just keep getting an error saying >preventDefault() is not a function.

Javascript Solutions


Solution 1 - Javascript

Try something like:

$('div.toggle').hide();
$('ul.product-info li a').click(function(event) {
    event.preventDefault();
    $(this).next('div').slideToggle(200);
});

Here is the page about that in the jQuery documentation

Solution 2 - Javascript

Set the href attribute as href="javascript:;"

<ul class="product-info">
  <li>
   <a href="javascript:;">YOU CLICK THIS TO SHOW/HIDE</a>
  <div class="toggle">
    <p>CONTENT TO SHOW/HIDE</p>
  </div>
 </li>
</ul>

Solution 3 - Javascript

It's suggested that you do not use return false, as 3 things occur as a result:

  1. event.preventDefault();
  2. event.stopPropagation();
  3. Stops callback execution and returns immediately when called.

So in this type of situation, you should really only use event.preventDefault();

Archive of article - jQuery Events: Stop (Mis)Using Return False

Solution 4 - Javascript

1 - Easy way:

<a href="javascript:;">Click Me</a>

2 - using void(0):

<a href="javascript:void(0);">Click Me</a>

3 - Using preventDefault():

<a href='#' onclick="event.preventDefault()">Click Me</a>

4 - Yet another way of doing this in Javascript using inline onclick, IIFE, event and preventDefault():

<a href='#' onclick="(function(e){e.preventDefault();})(event)">Click Me</a>

Solution 5 - Javascript

Alternatively, you could just return false from the click event:

 $('div.toggle').hide();
 $('ul.product-info li a').click(function(event){
  $(this).next('div').slideToggle(200);
+ return false; 
 });

Which would stop the A-Href being triggered.

Note however, for usability reasons, in an ideal world that href should still go somewhere, for the people whom want to open link in new tab ;)

Solution 6 - Javascript

This is a non-JQuery solution I just tested and it works.

<html lang="en">
<head>
<script type="text/javascript">
addEventListener("load",function(){
	var links= document.getElementsByTagName("a");
	for (var i=0;i<links.length;i++){
		links[i].addEventListener("click",function(e){
		alert("NOPE!, I won't take you there haha");
		//prevent event action
		e.preventDefault();
		})
	}
}); 

</script>
</head>
<body>
<div>
<ul>
	<li><a href="http://www.google.com">Google</a></li>
	<li><a href="http://www.facebook.com">Facebook</a></li>
	<p id="p1">Paragraph</p>
</ul>
</div>
<p>By Jefrey Bulla</p>
</body>
</html>

Solution 7 - Javascript

<ul class="product-info">
  <li>
    <a href="javascript:void(0);">YOU CLICK THIS TO SHOW/HIDE</a>
      <div class="toggle">
        <p>CONTENT TO SHOW/HIDE</p>
      </div>
  </li>
</ul>

Use

> javascript:void(0);

Solution 8 - Javascript

After several operations, when the page should finally go to <a href"..."> link you can do the following:

jQuery("a").click(function(e){
    var self = jQuery(this);
    var href = self.attr('href');
    e.preventDefault();
    // needed operations

    window.location = href;
});

Solution 9 - Javascript

Why not just do it in css?

Take out the 'href' attribute in your anchor tag

<ul class="product-info">
  <li>
    <a>YOU CLICK THIS TO SHOW/HIDE</a>
    <div class="toggle">
      <p>CONTENT TO SHOW/HIDE</p>
    </div>
  </li>
</ul>

In your css,

  a{
    cursor: pointer;
    }

Solution 10 - Javascript

If stopping the propagation of the event doesn't bother you, just use

return false;

at the end of your handler. In jQuery it prevents the default behaviour and it stop the event bubbling.

Solution 11 - Javascript

You can make use of return false; from the event call to stop the event propagation, it acts like an event.preventDefault(); negating it. Or you can use javascript:void(0) in href attribute to evaluate the given expression and then return undefined to the element.

Returning the event when it's called:

<a href="" onclick="return false;"> ... </a>

Void case:

<a href="javascript:void(0);"> ... </a>

You can see more about in: https://stackoverflow.com/questions/40048143/whats-the-effect-of-adding-void0-for-href-and-return-false-on-click-event-l/40048197#40048197

Solution 12 - Javascript

In Javascript you can use it

window.event.preventDefault();

It works in my case. write the above line into your function.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChris J AllenView Question on Stackoverflow
Solution 1 - JavascriptDavide GualanoView Answer on Stackoverflow
Solution 2 - JavascriptMusaddiq KhanView Answer on Stackoverflow
Solution 3 - JavascriptRyanView Answer on Stackoverflow
Solution 4 - JavascriptA-SharabianiView Answer on Stackoverflow
Solution 5 - JavascriptKent FredricView Answer on Stackoverflow
Solution 6 - JavascriptJefrey BullaView Answer on Stackoverflow
Solution 7 - JavascriptHATCHAView Answer on Stackoverflow
Solution 8 - Javascript1nstinctView Answer on Stackoverflow
Solution 9 - JavascriptsolwyvernView Answer on Stackoverflow
Solution 10 - JavascriptsebarmeliView Answer on Stackoverflow
Solution 11 - JavascriptRPichioliView Answer on Stackoverflow
Solution 12 - JavascriptMd Afsar UddinView Answer on Stackoverflow