I have this link:
<a href="#" id="myBtn"><i class="icon-user"></i><span>Add user</span></a>
I use preventDefault()
and return false
to avoid the #
at the end of my address. And it works perfectly. The problem is when I do the same with Bootstrap's dropdown. If I leave return false it doesn't add the #
at the end as supposed, but it also prevents the dropdown from disappearing as it would if I remove return false
.
This is my dropdown code:
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#" id="ddBtn">
<i class="icon-wrench"></i><span>Parent Item<b class="caret"></b></span>
</a>
<ul class="dropdown-menu">
<li><a href="#" id="ddSonBtn">Child item</a></li>
Javascript:
$("#ddSonBtn").click(function () {
//some code
e.preventDefault();
});
Is there a solution for this?
I am using
Bootstrap 2
jQuery
Thanks.
Prevent aims to protect and promote the fundamental British values, which include: 1 Democracy 2 Rule of Law 3 Tolerance 4 Individual Liberty More ...
8 Simple Steps to Prevent HIV 1 Know the Risks. 2 Take PrEP. 3 Stay Undetectable. 4 Use Condoms. 5 Conceive Safely. 6 Avoid Mother-to-Child Transmission. 7 Avoid Sharing Needles. 8 Prevent Infection After Exposure.
Good hygiene: the primary way to prevent infections 1 Wash your hands well. ... 2 Cover a cough. ... 3 Wash and bandage all cuts. ... 4 Do not pick at healing wounds or blemishes, or squeeze pimples. 5 Don't share dishes, glasses, or eating utensils. 6 Avoid direct contact with napkins, tissues, handkerchiefs, or similar items used by others.
Wash your hands often with soap and water or use an alcohol-based hand sanitizer, especially before eating or touching your face and after you use the bathroom. In Central and West Africa, avoid contact with animals that can spread monkeypox virus, usually rodents and primates.
According to the Bootstrap docs:
http://getbootstrap.com/javascript/#dropdowns
it is necessary to replace href
by data-target
attribute, i.e.
<a data-target="#" id="myBtn"><i class="icon-user"></i><span>Add user</span></a>
I believe it is more "bootstrapish" way to do things rather than suppressing event handlers or manually manipulating the show/hide behavior.
It may or may not be well documented, but there's no need to call both preventDefault()
and return false;
in jQuery events.
EDIT: From jQuery's on
docs: "Returning false from an event handler will automatically call event.stopPropagation()
and event.preventDefault()
" ( http://api.jquery.com/on/#event-handler )
return false;
is the equivalent of e.preventDefault();
and e.stopPropagation();
preventDefault
prevents the default behavior of an element - such as a submit button submitting a form, an anchor navigating to a specific href
, etc.
stopPropagation
stops the event from bubbling up the DOM. Meaning, events won't be triggered on parent elements of the target element.
Pick and choose which you need when working with Events. In your situation, you probably only need to call preventDefault
in your Dropdown's click handler.
Also, in your code, the use of return
in a function's scope is the last line of execution. Any code after it will not be executed when the function is run.
Just remove the #
value from the href
attribute. It's valid HTML to leave href
empty.
<a href="" id="myBtn"><i class="icon-user"></i><span>Add user</span></a>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With