Clean way to close dropdown list when clicking outside
I have a button that opens a dropdown list when clicking on.
I want to close this list when clicking outside the button. So I used a HostListener
like this:
@HostListener('click', ['$event.target'])
toggleDropdown(element)
const clickOnDropdownButton = (): boolean =>
const buttonChildsClasses = ['button is-primary tog-button', 'dropdown-label', 'tog-icon tog-icon-arrow-down', 'icon is-small'];
return buttonChildsClasses.indexOf(element.className) !== -1;
;
if (this.dropDownActive)
this.dropDownActive = false;
else if (!this.dropDownActive && clickOnDropdownButton())
this.dropDownActive = true;
This works but the use of a possible childs array is not clean at all in my opinion.
Indeed, the problem with my button is that it is composed of several HTML elements, my button isn't just a button
tag.
I thought of having a HTML element on top of those other elements using position: absolute
but I'm not sure if it's cleaner.
Any thoughts on this problem ?
javascript html angular
add a comment |
I have a button that opens a dropdown list when clicking on.
I want to close this list when clicking outside the button. So I used a HostListener
like this:
@HostListener('click', ['$event.target'])
toggleDropdown(element)
const clickOnDropdownButton = (): boolean =>
const buttonChildsClasses = ['button is-primary tog-button', 'dropdown-label', 'tog-icon tog-icon-arrow-down', 'icon is-small'];
return buttonChildsClasses.indexOf(element.className) !== -1;
;
if (this.dropDownActive)
this.dropDownActive = false;
else if (!this.dropDownActive && clickOnDropdownButton())
this.dropDownActive = true;
This works but the use of a possible childs array is not clean at all in my opinion.
Indeed, the problem with my button is that it is composed of several HTML elements, my button isn't just a button
tag.
I thought of having a HTML element on top of those other elements using position: absolute
but I'm not sure if it's cleaner.
Any thoughts on this problem ?
javascript html angular
I strongly recommend you use Angular CDK to implement dropdown component, its really very nice! you can see Example in my github github.com/100cm/at-ui-angular/blob/master/src/app/components/…
– junk
Nov 13 '18 at 9:58
the way i solved this was to add a click handler on the parent element of the dropdown (containing the dropdown opener button and the options list) and calling event.stopPropagation() there
– schellmax
Nov 13 '18 at 10:05
You can use a simple Jquery script and include that on the pages with dropdowns, nice thing is you can reuse this for all dropdowns all around your site. Maybe not the best option, but it is a simple option.
– Roboneter
Nov 14 '18 at 13:33
add a comment |
I have a button that opens a dropdown list when clicking on.
I want to close this list when clicking outside the button. So I used a HostListener
like this:
@HostListener('click', ['$event.target'])
toggleDropdown(element)
const clickOnDropdownButton = (): boolean =>
const buttonChildsClasses = ['button is-primary tog-button', 'dropdown-label', 'tog-icon tog-icon-arrow-down', 'icon is-small'];
return buttonChildsClasses.indexOf(element.className) !== -1;
;
if (this.dropDownActive)
this.dropDownActive = false;
else if (!this.dropDownActive && clickOnDropdownButton())
this.dropDownActive = true;
This works but the use of a possible childs array is not clean at all in my opinion.
Indeed, the problem with my button is that it is composed of several HTML elements, my button isn't just a button
tag.
I thought of having a HTML element on top of those other elements using position: absolute
but I'm not sure if it's cleaner.
Any thoughts on this problem ?
javascript html angular
I have a button that opens a dropdown list when clicking on.
I want to close this list when clicking outside the button. So I used a HostListener
like this:
@HostListener('click', ['$event.target'])
toggleDropdown(element)
const clickOnDropdownButton = (): boolean =>
const buttonChildsClasses = ['button is-primary tog-button', 'dropdown-label', 'tog-icon tog-icon-arrow-down', 'icon is-small'];
return buttonChildsClasses.indexOf(element.className) !== -1;
;
if (this.dropDownActive)
this.dropDownActive = false;
else if (!this.dropDownActive && clickOnDropdownButton())
this.dropDownActive = true;
This works but the use of a possible childs array is not clean at all in my opinion.
Indeed, the problem with my button is that it is composed of several HTML elements, my button isn't just a button
tag.
I thought of having a HTML element on top of those other elements using position: absolute
but I'm not sure if it's cleaner.
Any thoughts on this problem ?
javascript html angular
javascript html angular
edited Nov 13 '18 at 10:27
Krupesh Kotecha
2,06011134
2,06011134
asked Nov 13 '18 at 9:49
Baptiste ArnaudBaptiste Arnaud
7491726
7491726
I strongly recommend you use Angular CDK to implement dropdown component, its really very nice! you can see Example in my github github.com/100cm/at-ui-angular/blob/master/src/app/components/…
– junk
Nov 13 '18 at 9:58
the way i solved this was to add a click handler on the parent element of the dropdown (containing the dropdown opener button and the options list) and calling event.stopPropagation() there
– schellmax
Nov 13 '18 at 10:05
You can use a simple Jquery script and include that on the pages with dropdowns, nice thing is you can reuse this for all dropdowns all around your site. Maybe not the best option, but it is a simple option.
– Roboneter
Nov 14 '18 at 13:33
add a comment |
I strongly recommend you use Angular CDK to implement dropdown component, its really very nice! you can see Example in my github github.com/100cm/at-ui-angular/blob/master/src/app/components/…
– junk
Nov 13 '18 at 9:58
the way i solved this was to add a click handler on the parent element of the dropdown (containing the dropdown opener button and the options list) and calling event.stopPropagation() there
– schellmax
Nov 13 '18 at 10:05
You can use a simple Jquery script and include that on the pages with dropdowns, nice thing is you can reuse this for all dropdowns all around your site. Maybe not the best option, but it is a simple option.
– Roboneter
Nov 14 '18 at 13:33
I strongly recommend you use Angular CDK to implement dropdown component, its really very nice! you can see Example in my github github.com/100cm/at-ui-angular/blob/master/src/app/components/…
– junk
Nov 13 '18 at 9:58
I strongly recommend you use Angular CDK to implement dropdown component, its really very nice! you can see Example in my github github.com/100cm/at-ui-angular/blob/master/src/app/components/…
– junk
Nov 13 '18 at 9:58
the way i solved this was to add a click handler on the parent element of the dropdown (containing the dropdown opener button and the options list) and calling event.stopPropagation() there
– schellmax
Nov 13 '18 at 10:05
the way i solved this was to add a click handler on the parent element of the dropdown (containing the dropdown opener button and the options list) and calling event.stopPropagation() there
– schellmax
Nov 13 '18 at 10:05
You can use a simple Jquery script and include that on the pages with dropdowns, nice thing is you can reuse this for all dropdowns all around your site. Maybe not the best option, but it is a simple option.
– Roboneter
Nov 14 '18 at 13:33
You can use a simple Jquery script and include that on the pages with dropdowns, nice thing is you can reuse this for all dropdowns all around your site. Maybe not the best option, but it is a simple option.
– Roboneter
Nov 14 '18 at 13:33
add a comment |
1 Answer
1
active
oldest
votes
//this is your dropdown list in html
<div #dropdown> </div>
//this is your ts
@Component(
host: '(document:click)': 'handleClick($event)',
)
@ViewChild('dropdown', read: ElementRef) dropdown: ElementRef;
handleClick(event) {
var clickedComponent = event.target;
do
if (clickedComponent !== this.dropdown.nativeElement)
it's call when we click outside the dropdown list
while (clickedComponent);
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53278170%2fclean-way-to-close-dropdown-list-when-clicking-outside%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
//this is your dropdown list in html
<div #dropdown> </div>
//this is your ts
@Component(
host: '(document:click)': 'handleClick($event)',
)
@ViewChild('dropdown', read: ElementRef) dropdown: ElementRef;
handleClick(event) {
var clickedComponent = event.target;
do
if (clickedComponent !== this.dropdown.nativeElement)
it's call when we click outside the dropdown list
while (clickedComponent);
add a comment |
//this is your dropdown list in html
<div #dropdown> </div>
//this is your ts
@Component(
host: '(document:click)': 'handleClick($event)',
)
@ViewChild('dropdown', read: ElementRef) dropdown: ElementRef;
handleClick(event) {
var clickedComponent = event.target;
do
if (clickedComponent !== this.dropdown.nativeElement)
it's call when we click outside the dropdown list
while (clickedComponent);
add a comment |
//this is your dropdown list in html
<div #dropdown> </div>
//this is your ts
@Component(
host: '(document:click)': 'handleClick($event)',
)
@ViewChild('dropdown', read: ElementRef) dropdown: ElementRef;
handleClick(event) {
var clickedComponent = event.target;
do
if (clickedComponent !== this.dropdown.nativeElement)
it's call when we click outside the dropdown list
while (clickedComponent);
//this is your dropdown list in html
<div #dropdown> </div>
//this is your ts
@Component(
host: '(document:click)': 'handleClick($event)',
)
@ViewChild('dropdown', read: ElementRef) dropdown: ElementRef;
handleClick(event) {
var clickedComponent = event.target;
do
if (clickedComponent !== this.dropdown.nativeElement)
it's call when we click outside the dropdown list
while (clickedComponent);
answered Nov 13 '18 at 10:12
Ayush SahuAyush Sahu
13810
13810
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53278170%2fclean-way-to-close-dropdown-list-when-clicking-outside%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I strongly recommend you use Angular CDK to implement dropdown component, its really very nice! you can see Example in my github github.com/100cm/at-ui-angular/blob/master/src/app/components/…
– junk
Nov 13 '18 at 9:58
the way i solved this was to add a click handler on the parent element of the dropdown (containing the dropdown opener button and the options list) and calling event.stopPropagation() there
– schellmax
Nov 13 '18 at 10:05
You can use a simple Jquery script and include that on the pages with dropdowns, nice thing is you can reuse this for all dropdowns all around your site. Maybe not the best option, but it is a simple option.
– Roboneter
Nov 14 '18 at 13:33