Is there a way to 'undo' functions in jQuery? Let me explain.. I have a function which recreates exactly the select html tag. As you know with a select tag, clicking on a list item select that one, when clicking outside the select tag, the whole dropdown list closes. The selecting works fine, but the closing isn't. I though this would work with the event.stopPropagation()
event object but it doesn't. Is there any other solution for this?
Asked
Active
Viewed 1,252 times
-2

user1901096
- 247
- 4
- 15
-
1Not as such, no -- you have to build your own "undo" functions. – Blazemonger Jan 28 '13 at 14:19
-
Is there a particular reason you don't just use a ` – jbabey Jan 28 '13 at 14:19
-
@jbabey CSS styling, possibly. – Blazemonger Jan 28 '13 at 14:20
-
1If you're working with a plugin, it should have a destroy method that restores it to the original markup. – jrummell Jan 28 '13 at 14:20
-
@jbabey clicking the list opens another div with new options etc.. For this very project a unordered list is better, although i would also just use the – user1901096 Jan 28 '13 at 14:22
1 Answers
1
There is really nothing like what you are asking for that is built into jQuery natively. You have to do it on your own.
My suggestion would be to bind to the document:
$(document).on('click', function () {
$(".dropdownlist li").parent().children(":not(.selected)").hide();
});
You already have the stopPropagation
that would prevent this from being triggered when you click the list too.

Explosion Pills
- 188,624
- 52
- 326
- 405
-
Wouldn't the event have to be passed as an argument? Would resolving to window.event always work? – danronmoon Jan 28 '13 at 14:24
-
I had something similar with :not(.selected) but the issue is that everything disappears when clicking outside the
-
@user1901096 what do you mean by *everything*? Did you try my jsfiddle? – Explosion Pills Jan 28 '13 at 14:39
-
Hi, yes I've tried your jsfiddle. I meant that when the
- is expanded and I click outside of it the whole
- should be visible.
- closes and all is left is the red border whereas the selected
-
@user1901096 that doesn't happen to me; does it happen in a specific browser? – Explosion Pills Jan 28 '13 at 15:18
-
All browsers I've tested; Chrome v21 and FF v14 on Mac and IE8 in Windows. – user1901096 Jan 28 '13 at 15:28
-
@user1901096 I understand now. Problem is the `.removeClass` is done and then you never select an element again. This took a bit more updating: http://jsfiddle.net/cmAtc/2/ – Explosion Pills Jan 28 '13 at 15:43
-