Binding data-dismiss=“modal” to a key click
I have a simple modal that uses buttons to close it. The data-dismiss="modal" is binding to the button and it works perfectly fine.
I would like to bind the same functionality to my escape key and I'm wondering if its possible.
I tried something like the below but it does not work.
$(document).keyup(function (e)
if (e.which == 27)
$('#postcard-items-modal').modal('data-dismiss');
);
If anyone can shed some light on this that would be great.
I know I can add classes to hide the model or add a style tag to close it but im specifically looking to bind the data-dismiss to run when escape is pressed.
Thanks
javascript jquery bootstrap-modal
add a comment |
I have a simple modal that uses buttons to close it. The data-dismiss="modal" is binding to the button and it works perfectly fine.
I would like to bind the same functionality to my escape key and I'm wondering if its possible.
I tried something like the below but it does not work.
$(document).keyup(function (e)
if (e.which == 27)
$('#postcard-items-modal').modal('data-dismiss');
);
If anyone can shed some light on this that would be great.
I know I can add classes to hide the model or add a style tag to close it but im specifically looking to bind the data-dismiss to run when escape is pressed.
Thanks
javascript jquery bootstrap-modal
Possible duplicate question, answer can be found, here stackoverflow.com/questions/12630156/…
– cmprogram
Nov 14 '18 at 17:18
Thank you, that worked for me
– Davabo
Nov 15 '18 at 9:38
add a comment |
I have a simple modal that uses buttons to close it. The data-dismiss="modal" is binding to the button and it works perfectly fine.
I would like to bind the same functionality to my escape key and I'm wondering if its possible.
I tried something like the below but it does not work.
$(document).keyup(function (e)
if (e.which == 27)
$('#postcard-items-modal').modal('data-dismiss');
);
If anyone can shed some light on this that would be great.
I know I can add classes to hide the model or add a style tag to close it but im specifically looking to bind the data-dismiss to run when escape is pressed.
Thanks
javascript jquery bootstrap-modal
I have a simple modal that uses buttons to close it. The data-dismiss="modal" is binding to the button and it works perfectly fine.
I would like to bind the same functionality to my escape key and I'm wondering if its possible.
I tried something like the below but it does not work.
$(document).keyup(function (e)
if (e.which == 27)
$('#postcard-items-modal').modal('data-dismiss');
);
If anyone can shed some light on this that would be great.
I know I can add classes to hide the model or add a style tag to close it but im specifically looking to bind the data-dismiss to run when escape is pressed.
Thanks
javascript jquery bootstrap-modal
javascript jquery bootstrap-modal
asked Nov 14 '18 at 17:14
DavaboDavabo
60110
60110
Possible duplicate question, answer can be found, here stackoverflow.com/questions/12630156/…
– cmprogram
Nov 14 '18 at 17:18
Thank you, that worked for me
– Davabo
Nov 15 '18 at 9:38
add a comment |
Possible duplicate question, answer can be found, here stackoverflow.com/questions/12630156/…
– cmprogram
Nov 14 '18 at 17:18
Thank you, that worked for me
– Davabo
Nov 15 '18 at 9:38
Possible duplicate question, answer can be found, here stackoverflow.com/questions/12630156/…
– cmprogram
Nov 14 '18 at 17:18
Possible duplicate question, answer can be found, here stackoverflow.com/questions/12630156/…
– cmprogram
Nov 14 '18 at 17:18
Thank you, that worked for me
– Davabo
Nov 15 '18 at 9:38
Thank you, that worked for me
– Davabo
Nov 15 '18 at 9:38
add a comment |
2 Answers
2
active
oldest
votes
The Answer:
So I didn't need any javascript to bind a keyclick.
Add I had to do was add tabindex='-1' to my modal id
<div class="modal fade" role="dialog" id="PostcardModal" tabindex='-1'></div>
This will enable the escape button to close the modal.
Thank you to cmprogram.
add a comment |
Have you tried e.keyCode
instead of e.which
? Or you can try e.key
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%2f53305534%2fbinding-data-dismiss-modal-to-a-key-click%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The Answer:
So I didn't need any javascript to bind a keyclick.
Add I had to do was add tabindex='-1' to my modal id
<div class="modal fade" role="dialog" id="PostcardModal" tabindex='-1'></div>
This will enable the escape button to close the modal.
Thank you to cmprogram.
add a comment |
The Answer:
So I didn't need any javascript to bind a keyclick.
Add I had to do was add tabindex='-1' to my modal id
<div class="modal fade" role="dialog" id="PostcardModal" tabindex='-1'></div>
This will enable the escape button to close the modal.
Thank you to cmprogram.
add a comment |
The Answer:
So I didn't need any javascript to bind a keyclick.
Add I had to do was add tabindex='-1' to my modal id
<div class="modal fade" role="dialog" id="PostcardModal" tabindex='-1'></div>
This will enable the escape button to close the modal.
Thank you to cmprogram.
The Answer:
So I didn't need any javascript to bind a keyclick.
Add I had to do was add tabindex='-1' to my modal id
<div class="modal fade" role="dialog" id="PostcardModal" tabindex='-1'></div>
This will enable the escape button to close the modal.
Thank you to cmprogram.
answered Nov 15 '18 at 9:39
DavaboDavabo
60110
60110
add a comment |
add a comment |
Have you tried e.keyCode
instead of e.which
? Or you can try e.key
add a comment |
Have you tried e.keyCode
instead of e.which
? Or you can try e.key
add a comment |
Have you tried e.keyCode
instead of e.which
? Or you can try e.key
Have you tried e.keyCode
instead of e.which
? Or you can try e.key
answered Nov 14 '18 at 17:19
weibenfalkweibenfalk
55617
55617
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%2f53305534%2fbinding-data-dismiss-modal-to-a-key-click%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
Possible duplicate question, answer can be found, here stackoverflow.com/questions/12630156/…
– cmprogram
Nov 14 '18 at 17:18
Thank you, that worked for me
– Davabo
Nov 15 '18 at 9:38