I have a ul
list with 3 li
s inside it. these li
s are buttons to alter the margin of the ul
in order to create the effect of a slideshow. li
buttons are all working fine. but whenever ul is clicked, i.e., any area other than li
in ul
, i'm getting a wrong margin for ul
. wud u help me stop the event from firing on ul
?
Asked
Active
Viewed 78 times
-1

user3559645
- 23
- 5
-
2Code is worth 1024 words. – T.J. Crowder Apr 22 '14 at 09:01
-
Before you try to fix it, learn the right terminology of event bubbling and capturing [this question](http://stackoverflow.com/q/4616694/1671639). – Praveen Apr 22 '14 at 09:02
-
@T.J.Crowder, all i want is to prevent the execution of the event that causes the wrong margin to be applied to `ul`. this happens only when i click anywhere outside of `li`. anyway thanks for ur suggeation. – user3559645 Apr 22 '14 at 09:03
1 Answers
1
You can use event.stopPropagation():
Prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event.
For example:
$('ul').on('click','li',function(event) {
event.stopPropagation()
});

Felix
- 37,892
- 8
- 43
- 55
-
-
-
@user3559645, https://developer.mozilla.org/en-US/docs/Web/API/event.stopPropagation – Satpal Apr 22 '14 at 09:03
-
3@user3559645: This *is* JavaScript. It's using jQuery (a library, not a language), but then, you've tagged `jquery` in your question. If you want an answer using just the DOM, don't tag `jquery` in your question. – T.J. Crowder Apr 22 '14 at 09:03
-
-
-
so, if event.stopPropogation is called on ul, lis will still work fine? – user3559645 Apr 22 '14 at 09:06
-
2@user3559645: If you don't want to use jQuery, why did you tag the question with jQuery? – Felix Kling Apr 22 '14 at 09:07
-
-
-
1@user3559645: And how are the "jQuery experts" supposed to know that you don't want to use jQuery? – Felix Kling Apr 22 '14 at 09:09
-
-
@user3559645: *"i wanted all those jquery experts to answer this as well..."* That's called "tag spamming" and it's not remotely okay on SO. – T.J. Crowder Apr 22 '14 at 09:40